|
| 1 | +import pathlib |
| 2 | + |
| 3 | +# Let's create a basic path to this file. |
| 4 | +print(pathlib.Path(__file__)) |
| 5 | + |
| 6 | +""" |
| 7 | + Path classes are divided between pure paths, |
| 8 | + which provide purely computational operations |
| 9 | + without I/O, and concrete paths, which inherit |
| 10 | + from pure paths but also provide I/O operations. |
| 11 | +""" |
| 12 | + |
| 13 | +# Let's take at a Windows Path and a Pure Windows Path. |
| 14 | +windows_path = pathlib.WindowsPath(__file__) |
| 15 | +windows_path_pure = pathlib.PureWindowsPath(__file__) |
| 16 | +print("PurePath: " + str(windows_path_pure)) |
| 17 | +print("Path: " + str(windows_path)) |
| 18 | + |
| 19 | +""" |
| 20 | + Here's a case where this might be useful. You |
| 21 | + are on Linux but youneed to manipulate a windows |
| 22 | + path. |
| 23 | +
|
| 24 | + Let's explore the operations we can do with a path. |
| 25 | +""" |
| 26 | + |
| 27 | +# We can grab the parts. |
| 28 | +print(windows_path.parts) |
| 29 | + |
| 30 | +# There is also a property called `parent`. This will allow |
| 31 | +# us to get the directory above. |
| 32 | +print(windows_path.parent) |
| 33 | + |
| 34 | +# and you can attach one call on top of the other. |
| 35 | +print(windows_path.parent.parent) |
| 36 | + |
| 37 | +# But when you're doing that you really should use `parents`. |
| 38 | +# This way you can just select how you want to go up. For |
| 39 | +# example, if you want to recreate the code up above just |
| 40 | +# do this: |
| 41 | +print(windows_path.parents[1]) |
| 42 | + |
| 43 | +# I want to create a folder in this director, so let's define the path. |
| 44 | +data_folder = pathlib.Path(__file__).parents[0].joinpath('data') |
| 45 | + |
| 46 | +# Let's check to see if it already exists first, by using the `exists()` |
| 47 | +# method. |
| 48 | +if not data_folder.exists(): |
| 49 | + data_folder.mkdir() |
| 50 | + |
| 51 | +# Let's also add a file inside of it, but this time I want to raise |
| 52 | +# an error if it already exists. We don't to overwrite the file! |
| 53 | +data_folder.joinpath('my_file.txt').touch(exist_ok=True) |
| 54 | + |
| 55 | +# Lets add some more objects |
| 56 | +data_folder.joinpath('my_file.csv').touch(exist_ok=True) |
| 57 | +data_folder.joinpath('my_file.json').touch(exist_ok=True) |
| 58 | +data_folder.joinpath('my_folder').mkdir(exist_ok=True) |
| 59 | + |
| 60 | +# From here lets iterate through the folder. |
| 61 | +for file_obj in data_folder.iterdir(): |
| 62 | + |
| 63 | + # Let's ask some questions about each object. |
| 64 | + print("+"*80) |
| 65 | + print(file_obj) |
| 66 | + print("Are you a directory? " + str(file_obj.is_dir())) |
| 67 | + print("Are you a file? " + str(file_obj.is_file())) |
| 68 | + print("Are you a symbolic link? " + str(file_obj.is_symlink())) |
| 69 | + print("Are you a relative of the `Data` Folder? " + str(file_obj.is_relative_to(data_folder))) |
| 70 | + print("File Drive is: " + str(file_obj.drive)) |
| 71 | + print("File Stem is: " + str(file_obj.stem)) |
| 72 | + print("File Anchor is: " + str(file_obj.anchor)) |
| 73 | + print("File Name is: " + str(file_obj.name)) |
| 74 | + print("File Suffix is: " + str(file_obj.suffix)) |
| 75 | + |
| 76 | +# Here are some useful methods. |
| 77 | + |
| 78 | +# Grab the Current Working Directory. |
| 79 | +print("Current Working Directory: " + str(pathlib.Path.cwd())) |
| 80 | + |
| 81 | +# Grab the File Path of THIS script. |
| 82 | +print("Full File Path of the current Script: " + str(pathlib.Path(__file__))) |
| 83 | + |
| 84 | +# Grab the System home path. |
| 85 | +print("Home Path is: " + str(pathlib.Path.home())) |
| 86 | + |
| 87 | +# `absolute` takes a partial path and makes it a full path. |
| 88 | +print("My Partial Path looks like this before `absolute`: " + str(pathlib.Path("data"))) |
| 89 | +print("My Partial Path looks like this after `absolute`: " + str(pathlib.Path("data").absolute())) |
| 90 | + |
| 91 | +# `resolve` will do things like remove `..` or change windows path to unix paths and vice versa. |
| 92 | +print(pathlib.Path('docs/../setup.py').resolve()) |
| 93 | +print(pathlib.Path("C:/Users/Alex/OneDrive/Growth - Tutorial Videos/Lessons - Python/Lessons - Pathlib/using_pathlib.py").resolve()) |
| 94 | + |
| 95 | +# Final thing is `stat()` which can be used to get certain statistics about the file. |
| 96 | +print(pathlib.Path(__file__).stat().st_size) |
| 97 | +print(pathlib.Path(__file__).stat().st_mtime) |
0 commit comments