![]() |
While programming in any language, interaction between the programs and the operating system (Windows, Linux, macOS) can become important at some point in any developer’s life. This interaction may include moving files from one location to another, creating a new file, deleting a file, etc. In this article, we will discuss 10 essential file system methods of the OS and Shutil module in Python that helps to interact with our operating system and use OS-dependent functionalities. os.getcwd()os.getcwd() method tells us the location of the current working directory (CWD). Example: Python3
Output: Current working directory: /home/nikhil/Desktop/gfg os.chdir()os.chdir() method in Python used to change the current working directory to a specified path. It takes only a single argument as a new directory path. Python3
Output: Directory changed os.listdir()os.listdir() method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned. Example: Python3
Output: Files and directories in ' /home ' : ['nikhil'] os.walk()os.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). Example: Python3
Output: os.path.join()os.path.join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator (‘/’) following each non-empty part except the last path component. If the last path component to be joined is empty then a directory separator (‘/’) is put at the end. Python3
Output
/home/User/Desktop/file.txt /home/file.txt /home/User/Public/Documents/ os.makedirs()os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all. For example consider the following path: /home/User/Documents/GeeksForGeeks/Authors/nikhil Suppose we want to create a directory ‘nikhil’ but Directory ‘GeeksForGeeks’ and ‘Authors’ are unavailable in the path. Then os.makedirs() method will create all unavailable/missing directories in the specified path. ‘GeeksForGeeks’ and ‘Authors’ will be created first then ‘nikhil’ directory will be created. Example: Python3
Output: Directory 'nikhil' created shutil.copy2()shutil.copy2() method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil.copy() method but it also tries to preserve the file’s metadata. Example: Directory Used Python3
Output:
Directory shutil.move()shutil.move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory. If the destination already exists but is not a directory then it may be overwritten depending on os.rename() semantics. Example: Directory Used Python3
Output: os.remove()os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. Example: Python3
Output: file.txt has been removed successfully shutil.rmtree()shutil.rmtree() is used to delete an entire directory tree, path must point to a directory. Example: Suppose the directory and sub-directories are as follow. # Parent directory: # Directory inside parent directory: # File inside the sub-directory: Python3
Output:
|
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 10 |