[Python] File / directory operations

Since it is troublesome to check every time, I will summarize how to operate files / directories with Python.

Check if the path exists

In [1]: import os

Existing file

In [2]: os.path.exists("./test1/test1.txt")
Out[2]: True

Non-existent file

In [3]: os.path.exists("./test1/test1.doc")
Out[3]: False

Existing directory

In [4]: os.path.exists("./test1")
Out[4]: True

Non-existent directory

In [5]: os.path.exists("./test2")
Out[5]: False

Check if it is a file

File

In [6]: os.path.isfile("./test1/test1.txt")
Out[6]: True

directory

In [7]: os.path.isfile("./test1")
Out[7]: False

Non-existent file

In [8]: os.path.isfile("./test1/test1.doc")
Out[8]: False

Check if it is a directory

directory

In [9]: os.path.isdir("./test1")
Out[9]: True

File

In [10]: os.path.isdir("./test1/test1.txt")
Out[10]: False

Non-existent directory

In [11]: os.path.isdir("./test2")
Out[11]: False

Directory creation

In [12]: os.mkdir("./test2")

In [13]: ls
test1/  test2/

File copy

The metadata is not copied for copyfile and copy, but the metadata is copied for copy2. If you use copy2, the file creation date will also be copied, but copyfile and copy will have a new file creation date.

In [16]: import shutil

In [17]: shutil.copyfile("./test1/test1.txt", "./test2.txt")

In [18]: ls
test1/  test2.txt
In [19]: shutil.copy("./test1/test1.txt", "./test3.txt")

In [20]: shutil.copy2("./test1/test1.txt", "./test4.txt")

In [21]: ls
test1/  test1.txt  test2.txt  test3.txt  test4.txt

Copy the entire directory

When creating a new folder and copying

In [22]: shutil.copytree("./test1", "./test2")

In [23]: ls
test1/  test1.txt  test2/  test2.txt  test3.txt  test4.txt

In [24]: ls test1
test1.log  test10.txt  test2.txt   test3.log  test30.txt
test1.txt  test2.log   test20.txt  test3.txt  test4.txt

In [25]: ls test2
test1.log  test10.txt  test2.txt   test3.log  test30.txt
test1.txt  test2.log   test20.txt  test3.txt  test4.txt

I get an error when trying to copy to an existing folder

In [27]: os.mkdir("./test2")

In [28]: shutil.copytree("./test1", "./test2")

OSError: [Errno 17] File exists: './test2'

If you want to copy to an existing directory

In [29]: from distutils.dir_util import copy_tree

In [30]: copy_tree("./test1", "./test2")

In [31]: ls test1
test1.log  test10.txt  test2.txt   test3.log  test30.txt
test1.txt  test2.log   test20.txt  test3.txt  test4.txt

In [32]: ls test2
test1.log  test10.txt  test2.txt   test3.log  test30.txt
test1.txt  test2.log   test20.txt  test3.txt  test4.txt

File deletion

In [33]: os.remove("./test1.txt")

In [34]: ls
test1/  test2/  test2.txt  test3.txt  test4.txt

Directory deletion

To delete an empty directory An error occurs if there are files etc. in the directory

In [14]: os.rmdir("./test2")

In [15]: ls
test1/

When deleting the contents of the entire folder The contents of the folder may be empty.

In [35]: shutil.rmtree("./test2")

In [36]: ls
test1/  test2.txt  test3.txt  test4.txt

Delete only specific files

A combination of glob and remove

In [43]: ls test2
test1.log  test10.txt  test2.txt   test3.log  test30.txt
test1.txt  test2.log   test20.txt  test3.txt  test4.txt

In [44]: [os.remove(f) for f in glob.glob("./test2/*.log")]
Out[44]: [None, None, None]

In [45]: ls test2
test1.txt   test2.txt   test3.txt   test4.txt
test10.txt  test20.txt  test30.txt

Move file

In [52]: ls test2
test1.log  test10.txt  test2.txt   test3.log  test30.txt
test1.txt  test2.log   test20.txt  test3.txt  test4.txt

In [53]: shutil.move("./test2/test1.txt", ".")

In [54]: ls
test1/  test1.txt  test2/

In [55]: ls test2
test1.log   test2.log  test20.txt  test3.txt   test4.txt
test10.txt  test2.txt  test3.log   test30.txt

File / directory rename

File name change
In [56]: os.rename("./test1.txt", "./test2.txt")

In [57]: ls 
test1/  test2/  test2.txt

Directory name change

In [58]: os.rename("./test2", "./test3")

In [59]: ls
test1/  test2.txt  test3/

Get extension

In [60]: ftitle, fext = os.path.splitext('/path/to/test1.txt')

In [62]: fext
Out[62]: '.txt'

Get file title

In [60]: ftitle, fext = os.path.splitext('/path/to/test1.txt')

In [61]: ftitle
Out[61]: '/path/to/test1'

Get filename from full path

In [65]: os.path.basename('/path/to/test1.txt')
Out[65]: 'test1.txt'

Working with windows path on Linux doesn't work. Of course it's okay on Windows

In [66]: os.path.basename('\\path\\to\\test1.txt')
Out[66]: '\\path\\to\\test1.txt'

If you want to treat it as a Windows path on Linux

In [67]: import ntpath

In [68]: ntpath.basename('/path/to/test1.txt')
Out[68]: 'test1.txt'

It's okay if the Windows pass comes

In [69]: ntpath.basename('\\path\\to\\test1.txt')
Out[69]: 'test1.txt'

Split into directory name and file name

In [70]: os.path.split('/path/to/test1.txt')
Out[70]: ('/path/to', 'test1.txt')

If you want to treat it as a Windows path on Linux

In [71]: import ntpath

In [72]: ntpath.split('\\path\\to\\test1.txt')
Out[72]: ('\\path\\to', 'test1.txt')

In [73]: ntpath.split('/path/to/test1.txt')
Out[73]: ('/path/to', 'test1.txt')

Combine directory name and file name

In [74]: os.path.join('/path/to','test1.txt')
Out[74]: '/path/to/test1.txt'

Recommended Posts

[Python] File / directory operations
File operations in Python
File operations in Python
Summary of python file operations
File operations
Python memo ① Folder and file operations
ORC, Parquet file operations in Python
Python file processing
[Python3] Understand the basics of file operations
[Note] File reading ~ Python ~
File processing in Python
File operations with open — "../"
Automatically execute python file
Reading pyc file (Python 3.5.2)
Python directory operation summary
[Python] Summary of S3 file operations with boto3
Read Python csv file
python external file reading
[Python] Privately created & used small-scale functions (file operations, etc.)
Folder creation / file move / compress / delete operations with python
Summary of Python3 list operations
Four arithmetic operations in python
Download the file in Python
Draw netCDF file with python
Various Python built-in string operations
File access under the directory
Linux file and directory permissions
Wrapping git operations in Python
Create a directory with python
Basic commands for file operations
Get home directory with python
Download csv file with python
About Python and os operations
[Note] Import of a file in the parent directory in Python
Execute Python script from batch file
[Python] Check the current directory, move the directory
Extract the xz file with python
File / folder path manipulation in Python
Easy encryption of file contents (Python)
Python
Tips on Python file input / output
[Python] Write to csv file with Python
Perform Scala-like collection operations in Python
Save the binary file in Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
Linebot creation & file sharing in Python
Package filer for simple file operations
Output to csv file with Python
Create an Excel file with Python3
Create a binary file in Python
Create python directory Support if directory exists
Python CSV file reading and writing
Python os related files, subdirectory operations
[Linux] File and directory operation commands
Extract the targz file using python
Notes for Python file input / output
Python executable file conversion module comparison 2
Python CGI file created on Windows
Python import directory order (on anaconda)
[Automation with python! ] Part 2: File operation