The operation of files is a basic and important knowledge point in Python
. Whether it is crawling, data analysis, web development, or writing graphical interfaces, or data analysis, file-related operations may be required.
Today, I will study and review in summary, Python various file processing.
open()
mode open()
function The open (file name, operation mode)
function is used to open a file with a given file name in a specified mode.
Column name | column name |
---|---|
w | Write mode: the content of the file will be erased and rewritten |
r | Read mode: only read the content of the file |
a | Append mode: append content at the end of the file content |
w
mode to write file contentr
mode to read file contenta
mode to append file contentos
module os.listdir(directory name)
: It accepts a directory name as a parameter and returns a list containing all file names and subdirectory names in the directory; os.scandir(directory name)
: Its behavior is similar to listdir()
, but it returns an iterator of file objects instead of a string.pathlib
module pathlib.Path.iterdir()
: It works on a path
object and returns an iterator object similar to scandir()
.The folder structure is as follows:
os.listdir()
to get **os.scandir()
to get **pathlib
to get **There are a wealth of functions and methods in Python to obtain file information.
os
module os.stat (path string)
, given a file or folder path as a parameter, return a stat_result
object; os.scandir(directory name)
, this method we introduced above, it returns an iterable object, we traverse this iterable object, each traversed object has a stat()
method, its return value is the same as os.stat()
is the same.pathlib
module pathlib.Path.iterdir()
, each object in its return value has a stat()
method, which is the same as os.scandir()
.os.stat()
The returned stat_result()
object contains the following common information:
st_mode
, file mode: including file type and file mode bits (ie permission bits).
st_ino
, related to the platform, but if it is not zero, the file is uniquely identified according to the value of st_dev. usually:
On Unix, this value represents the inode number.
On Windows, this value represents the file index number.
st_dev
, the identifier of the device where the file is located.
st_nlink
, the number of hard links.
st_ui
, the user ID of the file owner.
st_gid
, the user group ID of the file owner.
st_size
, the file size (in bytes), the file can be a regular file or a symbolic link. The size of a symbolic link is the length of the path it contains, excluding the null byte at the end.
st_atime
, the most recent access time, in seconds.
st_mtime
, the most recent modification time, in seconds.
st_ctime
, depending on the platform:
Indicates the time of the most recent metadata change on Unix,
On Windows, it means the creation time, in seconds.
os.scandir()
You can still use the os
module and the pathlib
module to create a directory.
os
module os.mkdir (directory name)
, create a single directory through the given directory name; os.makedirs (directory path)
, create a complete directory tree;pathlib
module pathlib.Path.mkdir()
, create a directory from the given Path
object;os
module os.remove(file path)
, delete a single file, if the file does not exist, throw a FileNotFound
exception, if the path is a directory, throw an ʻIsADirectoryError` exception; os.unlink(file path)
, same as os.remove()
;pathlib
module pathlib.Path.unlink()
, delete the Path()
object file. os.rmdir (directory path)
, delete a specified directory path, if the directory is not empty, an OSError
exception will be thrown; pathlib.Path.rmdir()
, delete the path object. shutil.rmtree (directory path)
, delete a complete directory tree;Sometimes it is necessary to perform fuzzy query on file name pattern matching (including regular), and it is necessary to use functions and methods related to file name pattern matching.
. startswith()
and endswith()
methods, both of which operate on strings, and can be used when processing file names; fnmatch.fnmatch(file name, matching pattern)
, check whether the filename string matches the pattern string, and return True or False. glob.glob (matching pattern)
, returns a list of possible empty pathnames matching pathname, the elements of which must be strings containing path information. pathlib.Path.glob (matching pattern)
, similar to glob
, but based on the Path
object.fnmatch
os.walk (directory path, topdown=True)
, to generate file names in the directory tree by browsing the directory tree in the order of up->down or down->up. For each directory in the directory tree rooted at top (including top itself), it will generate a triple (directory path, directory name list, file name list). tempfile.TemporaryFile (mode)
to create and open a temporary file in the specified mode; tempfile.TemporaryDirectory()
, create a temporary directory and return it; shutil.copy(source, target)
, copy the source file to the target, if the target is a directory, use the same file name as the source; shutil.copytree (source directory, target directory)
, copy the entire directory tree starting from the source directory to the target directory and return to the target directory. shutil.move(source, target)
, move a file or directory from source to target; os.rename (current name, new name)
, rename a file or directory; zipfile.Zipfile (compressed package name, mode)
to open a compressed package in the specified mode; zipfile.Zipfile().extract(file name)
, extract the given file from the zip file; shutil.make_archive (base file name, format name, root path)
, compress and pack a folder as a compressed package from the given parameters; shutil.unpack_archive (compressed package file name, extracted directory)
, decompress a compressed package to the specified directory; fileinput.input([file name list])
, read each file from the given file name list;Recommended Posts