Since it is troublesome to check every time, I will summarize how to operate files / directories with Python.
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
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
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
In [12]: os.mkdir("./test2")
In [13]: ls
test1/ test2/
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
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
In [33]: os.remove("./test1.txt")
In [34]: ls
test1/ test2/ test2.txt test3.txt test4.txt
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
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
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 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/
In [60]: ftitle, fext = os.path.splitext('/path/to/test1.txt')
In [62]: fext
Out[62]: '.txt'
In [60]: ftitle, fext = os.path.splitext('/path/to/test1.txt')
In [61]: ftitle
Out[61]: '/path/to/test1'
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'
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')
In [74]: os.path.join('/path/to','test1.txt')
Out[74]: '/path/to/test1.txt'
Recommended Posts