Python Directory
Malini Shukla
Senior Data Scientist || Hiring || 6M+ impressions || Trainer || Top Data Scientist || Speaker || Top content creator on LinkedIn || Tech Evangelist
Python Directory
In a computer system, files are organized into directories. These may contain subdirectories and files. Indeed, this makes a vital part of a user-friendly UI. But don’t be confused; a dictionary is simply what you call a folder.So far, we’ve mostly only seen the computational capabilities of Python. Today, we’ll talk about how we can use it to handle Python directory. After this tutorial, you’ll be able to create, rename, list files in directory in Python, and work with python Directory. Before we proceed
Let’s revise Python Syntax.
we will import the OS module to be able to access the methods we will apply.
- >>> import os
Getting Current Python Directory
To find out which directory in python you are currently in, use the getcwd() method.
- >>> os.getcwd()
‘C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32’
Cwd is for current working directory in python. This returns the path of the current python directory as a string in Python. To get it as a bytes object, we use the method getcwdb().
- >>> os.getcwdb()
b’C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32′
Here, we get two backslashes instead of one. This is because the first one is to escape the second one since this is a string object.
- >>> type(os.getcwd())
- <class 'str'>
To render it properly, use the Python method with the print statement.
- >>> print(os.getcwd())
C:\Users\lifei\AppData\Local\Programs\Python\Python36-32
Changing Current Python Directory
To change our current working directories in python, we use the chdir() method. This takes one argument- the path to the directory to which to change.
- >>> os.chdir('C:\Users\lifei')
SyntaxError: (unicode error) ‘unicodeescape’ code can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
But remember that when using backward slashes, it is recommended to escape the backward slashes to avoid a problem.
- >>> os.chdir('C:\\Users\\lifei')
- >>> os.getcwd()
‘C:\\Users\\lifei’
When you restart the shell, we get back to the default working python directory.
- >>> os.chdir('C:\\Users\\lifei')
- >>> os.getcwd()
‘C:\\Users\\lifei’
You can also use forward slashes for the path. This way, you don’t have to use backward slashes to escape.
- >>> os.chdir('C:/Users/lifei')
- >>> os.getcwd()
‘C:\\Users\\lifei’
Finally, you can also use double quotes.
- >>> os.chdir("C:\\Users\\lifei")
Let us see a step-by-step guide to Install Python Windows
Python List Directories and Files
To get the contents of a directory into a python list, we use the listdir() method.
- >>> os.listdir()
[‘.atom’, ‘.eclipse’, ‘.idlerc’, ‘.p2’, ‘.tooling’, ‘.vscode’, ‘3D Objects’, ‘afiedt.buf’, ‘AppData’, ‘Application Data’, ‘Contacts’, ‘Cookies’, ‘Desktop’, ‘Documents’, ‘Downloads’, ‘Dropbox’, ‘eclipse’, ‘eclipse-workspace’, ‘eclipse-workspace-C++’, ‘eclipse-workspace-EE’, ‘Favorites’, ‘iCloudDrive’, ‘IntelGraphicsProfiles’, ‘Links’, ‘Local Settings’, ‘MicrosoftEdgeBackups’, ‘Music’, ‘My Documents’, ‘NetHood’, ‘NTUSER.DAT’, ‘ntuser.dat.LOG1’, ‘ntuser.dat.LOG2’, ‘NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TM.blf’, ‘NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TMContainer00000000000000000001.regtrans-ms’, ‘NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TMContainer00000000000000000002.regtrans-ms’, ‘ntuser.ini’, ‘OneDrive’, ‘Oracle’, ‘Pictures’, ‘PrintHood’, ‘Recent’, ‘Roaming’, ‘Saved Games’, ‘Searches’, ‘SendTo’, ‘Start Menu’, ‘Templates’, ‘Videos’, ‘workspace’]
Read more about the tremendous Python Career Opportunities in 2018
Note that this includes the hidden and system files as well.
- >>> os.chdir("C:\\Users\\lifei\\Desktop")
- >>> os.listdir()
[‘Adobe Photoshop CS2.lnk’, ‘Atom.lnk’, ‘Burn Book.txt’, ‘desktop.ini’, ‘Documents’, ‘Eclipse Cpp Oxygen.lnk’, ‘Eclipse Java Oxygen.lnk’, ‘Eclipse Jee Oxygen.lnk’, ‘For the book.txt’, ‘Items for trip.txt’, ‘Papers’, ‘Remember to remember.txt’, ‘Sweet anticipation.png’, ‘Today.txt’, ‘topics.txt’, ‘unnamed.jpg’]
This shows us the contents on the desktop. This was about Python List directory.
AI & Data | Technology | Data Engineer | Python Developer | Capital Markets & Formula 1 Enthusiast
6 年Short and concise.