Hello, I'm Chen Cheng~
In recent years, with the increasing popularity of python, people gradually use this programming language to carry out some automatic operations, so as to save the inefficiency caused by repeated labor. Then it will inevitably involve the operation of the file system, including file addition, deletion, modification, query and so on. Today's Xiaobian will introduce how to use Python to realize these functions
01 output current path
We can get the location of the current file through the OS Library in Python
import os os.getcwd()
02 splicing of paths
We passed OS path. Join () method
os.path.join('output', 'Book1.xlsx')
output
output\Book1.xlsx
03 confirm whether a folder or file exists
Let's first look at how to confirm whether a folder exists, also through the OS module
os.path.exists('directory_name')
In the same way, we can do this when we check whether a file exists
os.path.exists('path/file_name')
04 create folder directory
Then let's look at how to create a new folder
os.mkdir("Folder name")
Of course, if the folder directory already exists in advance, the above code will naturally report an error, so we usually check whether it already exists
if not os.path.exists('Folder name'): os.mkdir('Folder name')
Of course, sometimes we need to create subfolders under the created folder, such as the following figure
[]()
At this time, if you reference OS The MKDIR () method may be a little cumbersome. At this time, we can use OS Mkdirs () method
os.makedirs(os.path.join('test_dir', 'level_1', 'level_2', 'level_3'))
05 list all the files contained in the current directory
The code is as follows
os.listdir('Folder name')
But sometimes we may want to search for files ending in "py" in all files in the current directory. We can use wildcards. The code is as follows
list(glob(os.path.join('.', '*.py')))
The glob module mentioned above can quickly find the directories and files we want. It supports, * [] these four wildcards
06 moving files
What if we want to move files under different directories and folders? Here we introduce the shutil module in Python. Suppose we want to move several csv files in the current directory to the "test_dir" directory folder. The code is as follows
import shutil for file in list(glob(os.path.join('.', '*.csv'))): shutil.move(file, 'test_dir')
07 copying files
When we want to copy files, we can also use the shutil module. For example, we want to copy several csv files in the "test_dir" directory folder to the "output" directory folder. The code is as follows
shutil.copy(os.path.join('test_dir', 'data.csv'), 'output')
In addition, we can rename the files pasted in the past. The code is as follows
shutil.copy(os.path.join('test_dir', 'data.csv'), os.path.join('output', 'data_2.csv'))
08 delete file
Let's take a look at how to delete files? os. The remove () method can delete files,
os.remove(os.path.join('output', 'data_2.csv'))
When we want to delete an entire directory folder, we can use OS The rmdir () method, of course, is limited to empty directory folders
os.rmdir(os.path.join('test_dir', 'level_1', 'level_2', 'level_3'))
For the directory folder with files, which is not an empty directory folder, we still need to use the shutil module. The code is as follows
shutil.rmtree("test_delete")
Or
shutil.rmtree(os.path.join("test_delete", "test_1_delete"))
09 creating and decompressing compressed packages
When it comes to the contents related to the operation of compressed packages, we have to say the zipobj module
- Create a compressed package
Here we will use the write() method in the zipfile module
file_lists = list(glob(os.path.join('.', '*.xlsx'))) with zipfile.ZipFile(r"Compressed package I created.zip", "w") as zipobj: for file in file_lists: zipobj.write(file)
- Read the file information in the compressed package
It is implemented through the namelist() method in the zipfile module
with zipfile.ZipFile("Compressed package I created.zip", "r") as zipobj: print(zipobj.namelist())
output
['Book1.xlsx', 'supermarkt_sales.xlsx']
You can see that the output is several excel files we packed last time
- Extract the single file in the compressed package
It is implemented through the extract() method in the zipfile module
dst = "output" with zipfile.ZipFile("Compressed package I created.zip", "r") as zipobj: zipobj.extract("Book1.xlsx",dst)
The purpose of the above code is to unzip the "Book1.xlsx" file in the compressed package to the "output" directory folder
- Extract all the files in the compressed package
It is implemented through the extractall() method in the zipfile module. The code is as follows
dst = "output" with zipfile.ZipFile("Compressed package I created.zip", "r") as zipobj: zipobj.extractall(dst)
The above is what I share today. I hope it will help you!
Little friends who like this article, just praise and pay attention! Your must be a driving force for me to move forward!