11- Directories and files
Listen to this article
os module functions:
- os.getcwd() obtains current working directory
- os.chdir(dir) changes current working directory to dir
- os.listdir(dir=".") list files in directory dir
- os.path.exists(path) checks whether path exists
Print current directory:
import os # import modules
print (os.getcwd()) # e.g., "C:\Users\Azad"
Change the current directory to the home directory:
import os, os. path # import modules
home_dir = os.path.expanduser ("~") # get home directory
print(home_dir)
os.chdir (home_dir )
List all files in the home directory:
print(os.listdir())
# ['$netrc', '.anaconda', '.bash_history', '.bundle', ...]
If you like the content, please SUBSCRIBE to my channel for the future content