程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Illustration of Python | file and directory operations

編輯:Python

author : Han Xinzi @ShowMeAI
Tutorial address :http://www.showmeai.tech/tuto...
This paper addresses :http://www.showmeai.tech/article-detail/86
Statement : copyright , For reprint, please contact the platform and the author and indicate the source


1.Python File directory operation and OS modular

We're actually developing it , It is often necessary to read the file 、 Traverse 、 Modification and other operations , adopt python Standard built-in os modular , Be able to complete these operations in a concise and efficient way . Common operations are summarized as follows :

  • Folder operation : Including the creation of folders 、 modify ( Change of name / Move ), Inquire about ( see 、 Traverse )、 Delete etc. .
  • File operations : Including the creation of files 、 modify 、 Read 、 Delete etc. .
  • Path to the operation : Path operation of folder or file , Such as absolute path , File name and path split , Extension segmentation, etc

To complete the operation of files and directories , First, import the corresponding os modular , The code is as follows :

import os

2. Folder operation

Local pythontest Directory as demo Directory , The current files in this directory are as follows :

test
│ test.txt
└─test-1
test-1.txt

test And test-1 It's a folder. ,test.txt And test-1.txt It's a document .

(1) Query operation

stay linux We use ls / pwd / cd Wait to complete operations such as querying and switching paths , Corresponding python The operation method is as follows :

  • listdir : List of files and directories
  • getcwd : Get current directory
  • chdir : change directory
  • stat : Basic information of files and directories
  • walk : Recursively traversing directories
>>> os.chdir("./pythontest") # Change directory
>>> os.getcwd() # Get current directory
'/Users/ShowMeAI/pythontest'
>>> os.listdir("test") # List of files and directories , Relative paths
['test-1', 'test.txt']
>>> os.listdir("/Users/ShowMeAI/test") # List of files and directories , Absolute path
['test-1', 'test.txt']
>>> os.stat("test") # Get directory information
os.stat_result(st_mode=16877, st_ino=45805684, st_dev=16777221, st_nlink=11, st_uid=501, st_gid=20, st_size=352, st_atime=1634735551, st_mtime=1634735551, st_ctime=1634735551)
>>> os.stat("test/test.txt") # Get file information
os.stat_result(st_mode=33188, st_ino=45812567, st_dev=16777221, st_nlink=1, st_uid=501, st_gid=20, st_size=179311, st_atime=1634699986, st_mtime=1634699966, st_ctime=1634699984)

among stat The function returns the basic information of the file or directory , As follows :

  • st_mode: inode Protected mode
  • st_ino: inode Node number .
  • st_dev: inode Resident devices .
  • st_nlink: inode The number of links .
  • st_uid: The owner's user ID.
  • st_gid: Owner's group ID.
  • st_size: The size of a normal file in bytes
  • st_atime: Last visit .
  • st_mtime: Time of last modification .
  • st_ctime: Creation time .

In daily use , We usually use st_size 、st_ctime And st_mtime Get file size , Creation time , Modification time . in addition , We see that the output time is seconds , Let's talk about , About date conversion processing .

(2) Traversal operation

walk Function to recursively traverse the directory , return root,dirs,files, Corresponding to the current traversal directory , Subdirectories and files in this directory .

data = os.walk("test") # Traverse test Catalog
for root,dirs,files in data: # Recursive traversal and output
print("root:%s" % root)
for dir in dirs:
print(os.path.join(root,dir))
for file in files:
print(os.path.join(root,file))

(3) Create operation

  • mkdir : New single directory , If the parent directory in the directory path does not exist , The creation fails
  • makedirs : Create multiple directories , If the parent directory in the directory path does not exist , Automatically create
>>> os.mkdir("new")
>>> os.mkdir("new1/new1-1") # Parent directory does not exist , Report errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: The system could not find the specified path .: 'new1/new1-1'
>>> os.makedirs("new1/new1-1") # Parent directory does not exist , Automatically create
>>> os.listdir("new1")
['new1-1']

(4) Delete operation

  • rmdir : Delete a single empty directory , If the directory is not empty, an error will be reported
  • removedirs : Delete recursive multilevel empty directory by path , If the directory is not empty, an error will be reported
>>> os.rmdir("new1") # If the directory is not empty , Report errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: The directory is not empty .: 'new1'
>>> os.rmdir("new1/new1-1")
>>> os.removedirs("new1/new1-1") # Delete multi-level empty directory
>>> os.listdir(".")
['new']

Due to the restriction of deleting empty directories , It's more about use shutil Module rmtree function , You can delete non empty directories and their files .

(5) Modify the operating

  • rename : Rename a directory or file , You can modify the path of a file or directory ( That is, move operation ), If the target file directory does not exist , False report .
  • renames : Rename a directory or file , If the target file directory does not exist , Automatically create
>>> os.makedirs("new1/new1-1")
>>> os.rename("new1/new1-1","new1/new1-2") # new1-1 new1-2
>>> os.listdir("new1")
['new1-2']
>>> os.rename("new1/new1-2","new2/new2-2") # because new2 directory does not exist , Report errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: The system could not find the specified path .: 'new1/new1-2' -> 'new2/new2-2'
>>> os.renames("new1/new1-2","new2/new2-2") # renames Can automatically create non-existent directories
>>> os.listdir("new2")
['new2-2']

If the destination path file already exists , that os.rename() and os.renames() All will report wrong. :FileExistsError, When the file already exists , Unable to create the file .

3. File operations

(1) Query operation

  • open/read/close : File read
  • stat : file information , See... In the previous folder for details stat explain
>>> f = os.open("test/test.txt", os.O_RDWR|os.O_CREAT) # Open file
>>> str_bytes = os.read(f,100) # read 100 byte
>>> str = bytes.decode(str_bytes) # Byte to string
>>> print(str)
test write data
>>> os.close(f) # Close file 

Be careful open/read/close Need to operate together , among open The operation needs to specify the mode , The above is to open the file in read-write mode , If the file does not exist, create the file . The specific modes are as follows :

flags -- This parameter can be the following options , Multiple uses "|" separate :

  • os.O_RDONLY: Open as read-only
  • os.O_WRONLY: Open in write only mode
  • os.O_RDWR : Open by reading and writing
  • os.O_NONBLOCK: Open without blocking
  • os.O_APPEND: Open by appending
  • os.O_CREAT: Create and open a new file
  • os.O_TRUNC: Open a file and truncate it to zero length ( Must have write permission )
  • os.O_EXCL: If the specified file exists , Returns an error
  • os.O_SHLOCK: Automatically acquire shared locks
  • os.O_EXLOCK: Automatically acquire independent locks
  • os.O_DIRECT: Eliminate or reduce cache effects
  • os.O_FSYNC : Synchronous write
  • os.O_NOFOLLOW: Don't track soft links

(2) Create operation

Use open create a file , Specify the mode , If the file does not exist , Create . It's kind of similar linux In operation touch.

>>> f = os.open("test/ShowMeAI.txt", os.O_RDWR|os.O_CREAT) # If the file does not exist , Create
>>> os.close(f)

(3) Modify the operating

  • open/write/close : Write file contents
  • rename,renames : Same as the modified name described above 、 The movement operation is consistent .
>>> f = os.open("test/ShowMeAI.txt", os.O_RDWR|os.O_CREAT) # Open file
>>> os.write(f,b"ShowMeAI test write data") # Write content
15
>>> os.close(f) # Close file 

(4) Delete

  • remove : Delete file , Note that the directory cannot be deleted ( Use rmdir/removedirs)
>>> os.remove("test/test-1") # Error in deleting Directory
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: The system cannot find the specified file .: 'test/test1'
>>> os.remove("test/ShowMeAI.txt") # Delete file
>>> os.listdir("test")
['test-1']

4. Path to the operation

In the process of using files or directories , It is often necessary to deal with files and directory paths , therefore ,os There is a sub module in path, It is dedicated to processing path operations . The main operations are as follows :

  • abspath : Return to absolute path
>>> os.path.abspath("test")
'/Users/ShowMeAI/test'
  • exists : Determine if a file or directory exists
>>> os.path.exists("test")
True
>>> os.path.exists("test/test.txt")
False
>>> os.path.exists("test/test-1/test-1.txt")
True
  • isfile/isdir : Determine if it is a file / Catalog
>>> os.path.isdir("test")
True
>>> os.path.isfile("test/test-1/test-1.txt")
True
  • basename/dirname: Get the path tail and path header . In fact, it is the last one in the path / For the separator , Divided into head (head) And tail (tail) Two parts ,tail yes basename What's coming back ,head yes dirname What's coming back . Often used to get file names , Directory name and other operations
>>> os.path.basename("test/test-1/test-1.txt") # file name
'test-1.txt'
>>> os.path.basename("test/test-1/") # Empty content
''
>>> os.path.basename("test/test-1") # Directory name
'test-1'
>>> os.path.dirname("test/test-1/test-1.txt") # The path to the directory where the file is located
'test/test-1'
>>> os.path.dirname("test/test-1/") # Directory path
'test/test-1'
>>> os.path.dirname("test/test-1") # Parent directory path
'test'
  • join : Synthesis path , That is, connect the two parameters with the system path separator , Form a complete path .
>>> os.path.join("test","test-1") # Connect two directories
'test/test-1'
>>> os.path.join("test/test-1","test-1.txt") # Connect directory and filename
'test/test-1/test-1.txt'
  • split : Split file names and folders , Namely the path With the last slash "/" Separator , Cut into head and tail , With (head, tail) Returns... As a tuple .
>>> os.path.split("test/test-1") # Split Directory
('test', 'test-1')
>>> os.path.split("test/test-1/") # With / The end of the directory partition
('test/test-1', '')
>>> os.path.split("test/test-1/test-1.txt") # Split file
('test/test-1', 'test-1.txt')
  • splitext : Split path name and file extension , hold path Delimit with the last extension “.” Division , Cut into head and tail , With (head, tail) The situation of tuples returns . Pay attention to and split The difference is the separator .
>>> os.path.splitext("test/test-1")
('test/test-1', '')
>>> os.path.splitext("test/test-1/")
('test/test-1/', '')
>>> os.path.splitext("test/test-1/test-1.txt") # Distinguish between file name and extension
('test/test-1/test-1', '.txt')
>>> os.path.splitext("test/test-1/test-1.txt.mp4") # With the last "." For the point of division
('test/test-1/test-1.txt', '.mp4')

5. Typical applications

(1) Batch modify file name

def batch_rename(dir_path):
itemlist = os.listdir(dir_path)
# Get the list of catalog files
for item in itemlist:
# Connect into a full path
item_path = os.path.join(dir_path, item)
print(item_path)
# Change file name
if os.path.isfile(item_path):
splitext = os.path.splitext(item_path)
os.rename(item_path, splitext[0] + "-ShowMeAI" + splitext[1])

(2) Traverse all files with specified extension under the directory and subdirectory

def walk_ext_file(dir_path, ext_list):
# @dir_path Parameters : Traversal directory
# @ext_list Parameters : Extended name list , example ['.mp4', '.mkv', '.flv']
# Traverse
for root, dirs, files in os.walk(dir_path):
# Get the file name and path
for file in files:
file_path = os.path.join(root, file)
file_item = os.path.splitext(file_path)
# Output the file path with the specified extension
if file_item[1] in ext_list:
print(file_path)

(3) Sort the files in the specified directory according to the modification time

def sort_file_accord_to_time(dir_path):
# Before ordering
itemlist = os.listdir(dir_path)
print(itemlist)
# Forward order
itemlist.sort(key=lambda filename: os.path.getmtime(os.path.join(dir_path, filename)))
print(itemlist)
# Reverse sorting
itemlist.sort(key=lambda filename: os.path.getmtime(os.path.join(dir_path, filename)), reverse=True)
print(itemlist)
# Get the latest modified file
print(itemlist[0])

6. Video tutorial

Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition

https://www.bilibili.com/vide...

Data and code download

The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , Babies who can surf the Internet scientifically can also use google colab One click operation and interactive operation learning Oh !

This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :

  • Python Quick reference table

Expand references

  • Python course —Python3 file
  • Python course - Liao Xuefeng's official website

ShowMeAI Recommended articles

  • python Introduce
  • python Installation and environment configuration
  • python Basic grammar
  • python Basic data type
  • python Operator
  • python Condition control and if sentence
  • python Loop statement
  • python while loop
  • python for loop
  • python break sentence
  • python continue sentence
  • python pass sentence
  • python String and operation
  • python list
  • python Tuples
  • python Dictionaries
  • python aggregate
  • python function
  • python Iterators and generators
  • python data structure
  • python modular
  • python File read and write
  • python File and directory operations
  • python Error and exception handling
  • python object-oriented programming
  • python Namespace and scope
  • python Time and date

ShowMeAI A series of tutorials are recommended

  • The illustration Python Programming : From introduction to mastery
  • Graphical data analysis : From introduction to mastery
  • The illustration AI Mathematical basis : From introduction to mastery
  • Illustrate big data technology : From introduction to mastery


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved