Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python organize files

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge of how python organizes documents. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. Shutil module

Shutil is actually the shell module. It contains functions that allow us to copy, move, rename, and delete files in python programs.

1.1 copy files and folders

Shutil.copy (source,destination): copy the files at path source to the folder at path destination. If destination is a file name, it will be the new name to be copied

Shutil.copytree (source,destination): copy the folder at the path source, including all its files and subfolders, to the folder at the path destination.

Import shutilimport oscurrent_path = os.getcwd () # copy the test.txt file to the temp1 folder shutil.copy (current_path+ "/ test.txt", current_path+ "/ Python/temp1") # copy the contents of the temp1 folder to the temp2 folder, and the temp2 folder shutil.copytree (current_path+ "/ Python/temp1", current_path+ "/ Python/temp2") 1.2 will be created to move files and folders

Shutil.move (source,destination): moves the folder at source to the path destination and returns a string for the absolute path of the new location

Note:

If destination points to a folder, the source file will be moved to destination and the original file name will be maintained

If destination points to a file, the file will be overwritten. Note!

If destination points to a folder that does not exist, the contents of source will be moved to destination,source and renamed to destination

Import shutilimport oscurrent_path = os.getcwd () # copy the files in the temp2 folder to temp and rename temp2 to tempshutil.move (current_path+ "/ Python/temp2", current_path+ "/ temp") 1.3.Delete files and folders

Os.unlink (path): the file at path will be deleted

Os.rmdir (path): the folder at path will be deleted. The folder must be empty with no files and folders

Shutil.retree (path): the folder at path will be deleted, and all files and folders it contains will be deleted

Import shutilimport oscurrent_path = os.getcwd () shutil.rmtree (current_path+ "/ temp") II. Traversal files

Os.walk (path): by passing in a path

Os.walk () returns three values in each iteration of the loop:

1. The string of the current folder name

two。 List of strings for subfolders in the current folder

3. List of strings for files in the current folder

Import shutilimport oscurrent_path = os.getcwd () for folder_name,sub_folders,file_names in os.walk (current_path): print (folder_name+ ":") # load all child files under the current file path for sub_folder in sub_folders: print ("" + folder_name+ ":" + sub_folder+ "(dir)") for file_name in file_names: print ("" + folder_name+ ":" + file_name)

Output (part):

Ubuntu@VM-0-2Mub untuvu home/ubuntu/python_file/Python/orignize_files.py paste pythonites file$ / usr/bin/python3 / home/ubuntu/python_file/Python/orignize_files.py

/ home/ubuntu/python_file:

/ home/ubuntu/python_file: .vscode (dir)

/ home/ubuntu/python_file: Python (dir)

/ home/ubuntu/python_file: test.cpp

/ home/ubuntu/python_file: test.txt

/ home/ubuntu/python_file: tempCodeRunnerFile.py

/ home/ubuntu/python_file/.vscode:

/ home/ubuntu/python_file/.vscode: db (dir)

/ home/ubuntu/python_file/.vscode: .git (dir)

/ home/ubuntu/python_file/.vscode: log (dir)

/ home/ubuntu/python_file/.vscode: settings.json

/ home/ubuntu/python_file/.vscode/db:

/ home/ubuntu/python_file/.vscode/db: cpptips.db-wal

/ home/ubuntu/python_file/.vscode/db: cpptips.db-shm

/ home/ubuntu/python_file/.vscode/db: cpptips.db

/ home/ubuntu/python_file/.vscode/.git:

/ home/ubuntu/python_file/.vscode/.git: 6eb7a60f73d1a1d9bdf44f2e86d7f4cc_test.cpp

/ home/ubuntu/python_file/.vscode/log:

/ home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-19.log

/ home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-16.log

/ home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-17.log

/ home/ubuntu/python_file/.vscode/log: cpptips.client.log

/ home/ubuntu/python_file/.vscode/log: cpptips.server.log

/ home/ubuntu/python_file/Python:

/ home/ubuntu/python_file/Python: temp1 (dir)

/ home/ubuntu/python_file/Python: .git (dir)

/ home/ubuntu/python_file/Python: README.md

/ home/ubuntu/python_file/Python: hello_world.py

/ home/ubuntu/python_file/Python: orignize_files.py

/ home/ubuntu/python_file/Python: regex.py

/ home/ubuntu/python_file/Python: file_mange.py

.

.

.

III. Compressed files

Using the functions in the zipfile module, python programs can create and open ZIP files.

3.1 create and add ZIP files

To create a ZIP file, you must create a ZipFile object with the ZipFile method. The ZipFile object is similar in concept to the File object.

After that, open the object in write mode. Call the write () method to pass in a path, and python will compress the file referred to in that path and add it to the ZIP file. The first parameter to write is a string that represents the file name to be added. The second parameter is the Compression Type parameter, which tells the computer what algorithm to use to compress the file. Generally speaking, ZIP_DEFLATED is fine.

Import zipfileimport oscurrent_path = os.getcwd () new_zip = zipfile.ZipFile ("newZip", "w") new_zip.write ("test.txt", compress_type=zipfile.ZIP_DEFLATED) new_zip.write ("test.cpp", compress_type=zipfile.ZIP_DEFLATED) 3.2 read ZIP file

When you open a zip file with the ZipFile function, a ZipFile object is returned. You can then call the object's namelist () method to get a list of compressed files in zip.

At the same time, the object also has a getinfo () method, which can get some information about the file by passing in a compressed file name.

Import zipfileimport oscurrent_path = os.getcwd () new_zip = zipfile.ZipFile ("newZip", "r") files = new_zip.namelist () for file in files: info = new_zip.getinfo (file) print ("filename:" + file) print ("size:" + str (info.file_size)) print ("compress_size:" + str (info.compress_size))

Output:

Ubuntu@VM-0-2Mub untuvu home/ubuntu/python_file/Python/orignize_files.py paste pythonites file$ / usr/bin/python3 / home/ubuntu/python_file/Python/orignize_files.py

Filename: test.txt

Size: 26

Compress_size: 26

Filename: test.cpp

Size: 30

Compress_size: 28

3.3Unzip the ZIP file

The extractall method of the ZipFile object unzips all files and folders from the ZIP file and puts them in the current working directory:

Import zipfileimport oscurrent_path = os.getcwd () example_zip = zipfile.ZipFile ("newZip", "r") # decompress example_zip.extractall () example_zip.close () these are all the contents of the article "how python organizes files". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report