[PYTHON] File operations

python


import os
import pathlib
import glob
import shutil

print(os.path.exists('test.txt'))
#test.Determine if txt exists.
#Returns true if it exists, false if it does not exist.
print(os.path.isfile('test.txt'))
#test.Determine if txt is a file.
#Returns true if the file, false if it does not exist.
print(os.path.isdir('design'))
#Determine if design is a directory.
#Returns true if it is a directory, false if it is not a directory.

os.rename('test.txt', 'renamed.txt')
#test.txt renamed.Rename it to txt.
os.symlink('renamed.txt', 'symlink.txt')
#renamed.txt to symlink.Synchronize with txt.
#renamed.If you change the contents of txt
#symlink.The contents of txt are also renamed.It is changed to the same as txt.
#And vice versa.

os.mkdir('test_dir')
#test_Create a directory called dir.
os.rmdir('test_dir')
#test_Delete the directory called dir.(Cannot be deleted if it contains contents)

pathlib.Path('empty.txt').touch()
#empty.Create a file called txt.
os.remove('empty.txt')
#empty.Delete the file called txt.

os.mkdir('test_dir')
#test_Create a directory called dir.
os.mkdir(r'test_dir\test_dir2')
#test_test the directory dir2_Create in dir.
print(os.listdir('test_dir'))
#test_Output the directory in dir.

pathlib.Path(r'test_dir\test_dir2\empty.txt').touch()
#test_test in the dir directory_empty in the dir2 directory.Create a file called txt.

shutil.copy(r'test_dir\test_dir2\empty.txt',
            r'test_dir\test_dir2\empty2.txt')
#test_test in the dir directory_empty in the dir2 directory.txt file
#test_test in the dir directory_empty2 in the dir2 directory.Copy it with the name txt.

print(glob.glob(r'test_dir\test_dir2\*'))
#test_Output what is in dir2.

shutil.rmtree('test_dir')
#test_Remove dir(Can be deleted even if it contains contents)

print(os.getcwd())
#Output the directory location.

Recommended Posts

File operations
File operations in Python
[Python] File / directory operations
File operations with open — "../"
File operations in Python
Summary of python file operations
Basic commands for file operations
File matching
Package filer for simple file operations
Python memo ① Folder and file operations
File creation
ORC, Parquet file operations in Python
[Python3] Understand the basics of file operations
Script python file
UNIX file descriptor
syslog file rename
Duplicate file removal
Python file processing
[Python] Summary of S3 file operations with boto3
[Linux] File search
astropy: fits file
Nginx config file
Pathlib provides a common interface for file path operations
[Python] Privately created & used small-scale functions (file operations, etc.)
Folder creation / file move / compress / delete operations with python