Convenient features in the standard library since Python 3.4 (?) (Official documentation) Not only does it have a lot of alternatives to the os library, it's a great guy.
It's too much work and it's too good, so it's a lot of work to find out (too many features)
Therefore, I will quickly describe only how to use Path
, which I often use personally.
I would love to hear your comments if there is a better way.
#import
from pathlib import Path
#Object creation
p = Path(r"C:\Users\admin\Desktop\temp\hoge.txt")
#file name
print(path.name) # >> hoge.txt
#File name without extension
print(path.stem) # >> hoge
#Get only the extension
print(path.suffix) # >> .py
#At the beginning.Cut out if you don't need
print(path.suffix.lstrip(".")) # >> py
#Up to the parent hierarchy
print(path.parent) # >> C:\Users\admin\Desktop\temp
#Higher level (\..\..It's okay to combine them, but isn't it? )
print(path.parents[0]) # >> C:\Users\admin\Desktop\temp
print(path.parents[1]) # >> C:\Users\admin\Desktop
print(path.parents[2]) # >> C:\Users\admin
# os.path.join()Usage
path = Path("src", "python", "naritoblog") # src/python/naritoblog
#Join by operator
path1 = Path("src")
path2 = Path("python")
path3 = path1 / path2 / "naritoblog" # src/python/naritoblog
#Conversion to string
print(type(path3.__str__())) # >> str
#This is fine
print(type(str(path3))) # >> str
#Convert from relative path to absolute path
path1 = Path("hoge.txt")
print(path1) # >> hoge.txt
print(path1.resolve()) # >> C:\Users\admin\Desktop\temp\hoge.txt
path = Path("Taisho")
#Get all
list(path.glob("*"))
#File only
list(path.glob("*.*"))
#Directory only
[list for list in path.iterdir() if list.is_dir()]
With the Path
object, you don't even need to do complicated recursive processing!
#All of the following
list(path.glob("**/*"))
#The following files
list(path.glob("**/*.*"))
#Extension can be specified
list(path.glob("**/*.txt"))
#Directory only
[list for list in path.glob("**/*") if list.is_dir()]
Of course, not only .txt
but anything text-based.
txt_path = Path("xxx.txt")
#File creation (export)
with txt_path.open("w", encoding="utf-8") as file:
file.write("Test output")
#Read file
with txt_path.open("r", encoding="utf-8") as file:
print(file.read())
e? Is it long? Then over here
txt_path.write_text("Description", encoding="utf-8") #writing
src = txt_path.read_text(encoding="utf-8") #Read
Create a fictitious Path object and create its existence.
make_dir = Path("mkdir")
#parents: Created for each parent directory (mksirs-like)
# exist_ok: Can be created even if there is a folder with the same name (there is no error drop that exists)
make_dir.mkdir(parents=True, exist_ok=True)
# >>A folder called "mkdir" is created in the current directory
I don't say remove to delete files. The era is unlink. ~~ Words you want to speak out ~~
#Delete file
a_txt = Path('xxx.txt')
a_txt.unlink()
#Delete directory (only empty)
rm_dir = Path('mkdir')
rm_dir.rmdir()
In short, change only the name
and suffix
attributes of the Path
object
Note that it is the information of the Path
object that changes, not the actual file.
If you want to change the actual situation, use ʻos.rename` etc. (Is it possible with pathlib?)
There is also a similar function, .rename ()
, but it was strangely difficult to use, so I omitted it.
#Rename file (name attribute)
path_a = Path(r"C:\temp\a_text.txt")
path_b = path_a.with_name("b_text.txt")
print(path_a) # >> C:\temp\a_text.txt
print(path_b) # >> C:\temp\b_text.txt
#Change extension (suffix attribute)
path_a = Path(r"C:\temp\wich_test.txt")
path_b = path_a.with_suffix(".csv")
print(path_a) # >> C:\temp\wich_test.txt
print(path_b) # >> C:\temp\wich_test.csv
Conclusion
pathlib
Recommended Posts