Python OS operation

1. List of methods of os module

No. Method Description
1 os.sep Get the path delimiter in the corresponding OS. Linux/Unix is "/, Windows is "\」
2 os.name Get the name of the corresponding OS. Linux/Unix is "posix", Windows is "nt"
3 os.getcwd() Get the current path. Equivalent to the Linux command "pwd"
4 os.getenv(env_name) Get the environment variable by the specified environment variable name
5 os.listdir(path) Get all folder names and file names directly under the specified path as an array. If no path is specified, the current path is adopted as the default value.
6 os.remove(file_name) Delete the specified file
7 os.stat(file_name) Gets the attributes of the specified file. Attribute details:http://www.w3big.com/ja/python/os-stat.html
8 os.chmod(path, mode) Change the access authority of the specified file or folder.
Example:os.chmod('f1.txt', 0o664)
Note: mode is an octal number, so you need to specify "0o" at the beginning.
9 os.mkdir(dir_name) Create a folder
10 os.rmdir(dir_name) Delete the specified folder
11 os.linesep Get the line feed code for the corresponding OS. Linux is "\n ", Unix is"\r ", Windows is"\r\n」
12 os.path.isfile(path) Determine if the specified path is a file
13 os.path.isdir(path) Determine if the specified path is a folder
14 os.path.exists(path) Determine if the specified path exists
15 os.path.getsize(file_name) Get the size of the specified file
16 os.path.abspath(file_name) Get the absolute path of the specified file
17 os.path.isabs(file_path) Determines if the specified file path is an absolute path
18 os.path.splitext(file_name) Divide the name and extension of the specified file and get it as Tuple type
19 os.path.join(dir_path, file_name) Combine folder path and file name
20 os.path.basename(file_path) Get the file name of the specified path
21 os.path.dirname(file_path) Get the folder path of the specified file

2. List of methods of shutil module

■ shutil.copyfileobj(fsrc, fdst[, length])

Copy the contents of the file. The fdst file must exist in advance.

shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

■ shutil.copyfile(src, dst)

Copy the file. The dst file does not need to exist in advance.

shutil.copyfile('f1.log', 'f2.log')

■ shutil.copymode(src, dst)

Copy the permissions on the file. The dst file must exist in advance.

shutil.copymode('f1.log', 'f2.log')

■ shutil.copystat(src, dst)

Copy the attributes of the file.

os.stat('f1.txt')
>>> os.stat_result(st_mode=33206, st_ino=4067417, st_dev=66309, st_nlink=1, st_uid=1000, st_gid=1000, st_size=11, st_atime=1592114811, st_mtime=1592113011, st_ctime=1592114642)

os.stat('f4.txt')
>>> os.stat_result(st_mode=33188, st_ino=4065524, st_dev=66309, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1592115009, st_mtime=1592115009, st_ctime=1592115009)

shutil.copystat('f1.txt', 'f4.txt')

os.stat('f4.txt')
>>> os.stat_result(st_mode=33206, st_ino=4065524, st_dev=66309, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1592114811, st_mtime=1592113011, st_ctime=1592115138)

■ shutil.copy(src, dst)

Copy the contents of the file and permissions. shutil.copy = shutil.copyfile + shutil.copymode

shutil.copy('f1.log', 'f2.log')

■ shutil.copy2(src, dst)

Copy the contents and attributes of the file. shutil.copy2 = shutil.copyfile + shutil.copystat

shutil.copy2('f1.log', 'f2.log')

■ shutil.copytree(src, dst, symlinks=False, ignore=None)

Recursively copy the contents of the folder. The default is not to copy the system link file, it can be changed with the ** symlinks ** parameter. The ** ignore ** parameter allows you to specify the pattern of files you do not want to copy.

#Copy the contents of dir01 to dir02 and ignore files with the extension "txt" and the beginning "tmp"
ignore_pattern = shutil.ignore_patterns('*.txt', 'tmp*')
shutil.copytree('dir01', 'dir02', ignore=ignore_pattern)

■ shutil.rmtree(path[, ignore_errors[, onerror]])

Recursively delete folders

shutil.rmtree('dir04')

■ shutil.move(src, dst)

Move files and folders

shutil.move('folder1', 'folder3')

■ shutil.make_archive(base_name, format,...)

Compress the file.

--base_name: Compressed file name --format: Compressed format --"zip, tar, bztar, gztar" --root_dir: Folder path to be compressed (default is the current path)

shutil.make_archive('data', 'gztar', root_dir='/data')

Recommended Posts

Python OS operation
[python] vector operation
[Python] Matrix operation
My os (python)
[Python] Operation of enumerate
Python directory operation summary
Python logical operation stumbling
Python decorator operation memo
[python] Array slice operation
Python
S3 operation with python boto3
About Python and os operations
Platform (OS) determination by Python
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Python pywin32 (win32com) Excel operation memorandum
[python] week1-3: Number type and operation
OS determination by Makefile using Python
Python os related files, subdirectory operations
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
Basic Python operation 2nd: Function (argument)
Determining which OS is running Python
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
[Automation with python! ] Part 2: File operation
[Python] File operation using if statement
kafka python
Mouse operation using Windows API in Python
Python basic operation 1st: List comprehension notation
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python + Selenium Frequently used operation method summary
Python comprehension
install python
Python Singleton
Bit operation
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec