Manipulate files and folders in Python

Content of this article: About file / folder operations with python

I investigated how to operate the following files and folders using the standard python modules os and shutil, so I summarized them.

-How to create a directory -How to copy the entire contents of the directory -How to delete files -How to delete the entire contents of the directory -How to delete an empty directory

How to create a directory

You can create a directory with the os.mkdir () function. If the directory already exists, you will get an error.

import os
os.mkdir("fold")

How to copy the entire contents of a directory

You can use the shutil.copytree () function to copy an existing directory to another directory with its contents.

import shutil
shutil.copytree("fold1","fold2") #Copy fold1 as fold2

How to delete a file

You can remove the file with the os.remove () function.

import os
os.remove("file.txt") #"file.txt"Delete

How to delete the entire contents of a directory

You can delete the entire directory with the shutil.rmtree () function.

import shutil
shutil.rmtree("fold1") #"fold1"Delete the entire contents

How to delete an empty directory

You can delete an empty directory with the os.rmdir () function.

import os
os.rmdir("fold1") #"fold1"Delete

Recommended Posts

Manipulate files and folders in Python
Export and output files in Python
Read and write JSON files in Python
Reading and writing CSV and JSON files in Python
Stack and Queue in Python
Unittest and CI in Python
Include and use external Kv files in Python Kivy
Recursively search for files and directories in Python and output
Transpose CSV files in Python Part 1
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
About dtypes in Python and Cython
Read and use Python files from Python
Handling of JSON files in Python
Download Google Drive files in Python
Assignments and changes in Python objects
Check and move directories in Python
Manipulate the clipboard in Python and paste the table into Excel
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Sort large text files in Python
Manipulate OCTA udf filenames in Python
Hashing data in R and Python
Allow Python to select strings in input files from folders
Function synthesis and application in Python
Read files in parallel with Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Extract strings from files in Python
How to use functions in separate files Perl and Python versions
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
[Linux] How to subdivide files and folders
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
[Automation] Manipulate mouse and keyboard with Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Find files like find on linux in Python
Implementation module "deque" in queue and Python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Type annotations for Python2 in stub files!
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Referencing INI files in Python or Ruby
Automate jobs by manipulating files in Python
Reading and writing JSON files with Python
Dealing with "years and months" in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]