Python memo ① Folder and file operations

Basic operation of files and folders by python

This is a memo for myself. The version is python3.7.1. I will add it as needed.

Create folder

make_folder.py


# make folder
import os

def make_folder(path):
  if os.path.exists(path)==False:
    os.mkdir(path)
    

Get only files

get_files.py


import os
import sys

def get_images(path):
  folder = os.listdir(path):
  files = [f for f in folder if os.path.isfile(os.path.join(path, f))]
  if len(files)==0:
    print("File does not exist")
    sys.exit()
  return files

Get the name and extension of the file

get_filename.py



name, ext =  os.path.splitext(file)

Recommended Posts

Python memo ① Folder and file operations
See file and folder information on python
File operations in Python
[Python] File / directory operations
File operations in Python
About Python and os operations
Folder creation / file move / compress / delete operations with python
Difference between java and python (memo)
File / folder path manipulation in Python
python memo
Python memo
python memo
Python CSV file reading and writing
ORC, Parquet file operations in Python
File operations
Python memo
Python memo
Statistical basics and Python, graphing, etc. (memo)
A memo with Python2.7 and Python3 on CentOS
Copy file and rewrite cell value @python
Write O_SYNC file in C and Python
Python data structure and operation (Python learning memo ③)
[python] Read html file and practice scraping
[Python3] Understand the basics of file operations
[Python] Memo dictionary
Script python file
python beginner memo (9.2-10)
A memo organized by renaming the file names in the folder with python
python beginner memo (9.1)
Python file processing
★ Memo ★ Python Iroha
[Python] EDA memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
[Python] Numpy memo
File DL, byte value and delete in Python3
Output python log to both console and file
[Python] Summary of S3 file operations with boto3
Reading and writing fits files with Python (memo)
Get the update date of the Python memo file.
[Keras] Personal memo to classify images by folder [Python]
Python class (Python learning memo ⑦)
Python and numpy tips
The simplest Python memo in Japan (classes and objects)
[Python] pip and wheel
python openCV installation (memo)
Python module (Python learning memo ④)
[Note] File reading ~ Python ~
File processing in Python
Visualization memo by Python
Try creating a compressed file using Python and zlib
Batch design and python
Python iterators and generators
Python packages and modules
Python test package memo
Vue-Cli and Python integration
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
Automatically execute python file
[Python] Memo about functions