Python Directory
Directories in Python

Python Directory

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.

  1. >>> import os

Getting Current Python Directory

To find out which directory in python you are currently in, use the getcwd() method.

  1. >>> 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().

  1. >>> 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.

  1. >>> type(os.getcwd())
  2. <class 'str'>

To render it properly, use the Python method with the print statement.

  1. >>> 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.

  1. >>> 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.

  1. >>> os.chdir('C:\\Users\\lifei')
  2. >>> os.getcwd()

‘C:\\Users\\lifei’

When you restart the shell, we get back to the default working python directory.

  1. >>> os.chdir('C:\\Users\\lifei')
  2. >>> 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.

  1. >>> os.chdir('C:/Users/lifei')
  2. >>> os.getcwd()

‘C:\\Users\\lifei’

Finally, you can also use double quotes.

  1. >>> 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.

  1. >>> 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.

  1. >>> os.chdir("C:\\Users\\lifei\\Desktop")
  2. >>> 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.

Reasons to Learn Python Programming Language.

Read Complete Article>>

Nivesh Sanghvi

AI & Data | Technology | Data Engineer | Python Developer | Capital Markets & Formula 1 Enthusiast

6 年

Short and concise.

回复

要查看或添加评论,请登录

Malini Shukla的更多文章

社区洞察

其他会员也浏览了