13.1.4. Pathlib Module#
Module to use same path for different OS’s
13.1.5. Older ways#
defining paths manually
data_folder = “source_data\text_files"
file_to_open = data_folder + “raw_data.txt”
f = open(file_to_open)
using os.path module
data_folder = “source_data\text_files"
file_to_open = data_folder + “raw_data.txt”
f = open(file_to_open)
13.1.6. Pathlib#
Check easily if file exists
To deal with file extensions
Common operations with paths
Inbuilt in python 3.4 ,so no need to install
treat paths as obects
from pathlib import Path
import os
Path() # If no arguments passed,Current folder
# relative path to the current folder
PosixPath('.')
Path('_build/subfolder')
PosixPath('_build/subfolder')
13.1.7. Creating a path by passing folder and file name#
Path("_build/subfolder", "file1.json")
PosixPath('_build/subfolder/file1.json')
13.1.8. Get home directory#
home_dir=Path.home() # In windows,home directory is C:/Users:/Username
home_dir
PosixPath('/Users/sahilchoudhary')
13.1.9. Get Current working directory#
cwd=Path.cwd()
print(str(cwd))
/Users/sahilchoudhary/Desktop/Projects/Digital Ebook/Draft-Codes-main/Chapter files
13.1.10. Change current working directory#
os.chdir('C:\\users\\Sahil Choudhary\\Desktop') # Most important command
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 os.chdir('C:\\users\\Sahil Choudhary\\Desktop')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\users\\Sahil Choudhary\\Desktop'
13.1.11. Get the parent directories#
# Get the first parent
one_above=Path.cwd().parent
print(one_above)
n_above=Path.cwd().parents[0]
n1_above=Path.cwd().parents[1]
print(n_above)
print(n1_above)
C:\Users\Sahil Choudhary\Book
C:\Users\Sahil Choudhary\Book
C:\Users\Sahil Choudhary
13.1.12. Joining paths#
joined_path=cwd/'Output'
# joined_path=cwd/'Output'/'Subfolder'
joined_path
WindowsPath('C:/Users/Sahil Choudhary/Book/Python/Output')
13.1.13. Create folder if not exists#
Path.cwd().mkdir(exist_ok=True)
13.1.14. Get file name with extension#
example_file=cwd/'Python .ipynb'
example_file.name
'Python .ipynb'
13.1.15. Check if file exists#
example_file.is_file()
False
13.1.16. Get file name without extension#
example_file.stem
'Python '
13.1.17. Get only the extensions#
example_file.suffix
'.ipynb'
13.1.18. Iterate over all the files in directory#
folder=cwd/"_build/subfolder"
for file in folder.iterdir():
print(file)
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Array Destructuring.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\BOOLEAN TITLE.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Break and Continue.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Changing data type-Lists.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Changing Datatypes.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\DateTime in Pandas.ipynb
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\DateTime.ipynb
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\file1.txt
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\file2.txt
13.1.19. Iterate over specific file types#
for file in folder.iterdir():
if file.suffix=='.html':
print(file)
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Array Destructuring.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\BOOLEAN TITLE.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Break and Continue.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Changing data type-Lists.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Changing Datatypes.html
13.1.20. Iterate over files inclusive of subdirectories#
for file in folder.rglob("*"):
print(file)
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Array Destructuring.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\BOOLEAN TITLE.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Break and Continue.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Changing data type-Lists.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\Changing Datatypes.html
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\DateTime in Pandas.ipynb
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\DateTime.ipynb
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\file1.txt
C:\Users\Sahil Choudhary\Book\Python\_build\subfolder\file2.txt
13.1.21. Summary#
from pathlib import Path # Now,we have path function which has certain important attributes
Path.cwd()
WindowsPath('C:/Users/Sahil Choudhary/Book/Python')
# OLD METHODS TO DEFINE PATHS
# path1="C:\Users\Sahil Choudhary\Book\Python\_build\subfolder" # This will give error
path2=r"C:\Users\Sahil Choudhary\Book\Python\_build\subfolder" # r means raw string.Should be added before paths
path3="C:\\Users\\Sahil Choudhary\\Book\\Python\\_build\\subfolder" # works in windows
path4="_build\subfolder" # relative path
path5="_build\\subfolder" # relative path
C:\Users\Sahil Choudhary
# NEW METHOD TO DEFINE PATHS
path6=Path('_build/subfolder') # relative path in pathlib
for file in path6.iterdir():
print(file)
_build\subfolder\Array Destructuring.html
_build\subfolder\BOOLEAN TITLE.html
_build\subfolder\Break and Continue.html
_build\subfolder\Changing data type-Lists.html
_build\subfolder\Changing Datatypes.html
_build\subfolder\DateTime in Pandas.ipynb
_build\subfolder\DateTime.ipynb
_build\subfolder\file1.txt
_build\subfolder\file2.txt