MITB Banner

12 Essential File System Functions You Should Know In Python

Share

Source: Python Programmers

The effectiveness of a product especially software, relies on how organised the basic building blocks are. With Python being used for all complex mathematical and machine learning processes, the aspect of file finding and handling might seem trivial or unattractive from the birds eye view. But documentation becomes crucial when the content of the product has to be worked on by a different team.

For instance, specific file documentations– these are usually generated through a third party script which will parse a file and, based on the comment blocks, will create an explicit PDF. Afterwards there should be information regarding the code repository, where the file updates are found, and where they need to be moved.

Here are few important file system methods that make Python easier to use:

f.write(string)

This method is used to write the contents of string to the file, returning the number of characters written.

os.path()

To test if a path exists (be it a file, directory or even link), use os.path.exists() is used.

import os.path

path_to_file = “/path/to/file”

if os.path.isfile(path_to_file):

This method returns True if the given path exists and it’s a file.

os.getcwd()

This method returns the current working directory as a string.

os.listdir()

os.listdir() returns the contents of the current working directory as a list of strings.

os.walk()

Creates a generator that can return information about the current directory and subdirectories. It works through the directories in the specified starting directory.

os.walk() returns the following items for each directory it traverses:

  • current directory path as a string
  • subdirectory names in the current directory as lists of strings
  • filenames in current directory as a list of strings

It’s often useful to use os.walk() with a for loop to iterate over the contents of a directory and its subdirectories.

import os

cwd = os.getcwd()

for dir_path, dir_names, file_names in os.walk(cwd):

  for f in file_names:

      print(f)

os.chdir()

This method changes the current working directory to either the absolute or relative path provided.

It’s a good idea to handle any exceptions raised when using this method with try-except. Otherwise there is a chance of deleting directories or files you don’t want deleted.

os.path.join()

The os.path module has a number of useful methods for common pathname manipulations. You can use it to find information about directory names and parts of directory names. The module also has methods to check whether a file or directory exists.

os.path.join() is designed to create a path that will work on most any operating system by joining multiple strings into one file path.

os.makedirs()

os.makedirs() makes directories. The mkdir() method also makes directories, but it does not make intermediate directories. So I suggest you use os.makedirs().

shutil.copy2()

This method can be used to copy a file or directory .shutil.copy2()is a good choice because it tries to preserve as much of the source file’s metadata as possible.

shutil.copy2(“source_file_path”,”destination_directory_path”) 

shutil.move()

To move a file or directory — mv

shutil.move() can be used to change a file’s location. It uses copy2 as the default under the hood.

shutil.move(“source_file_path”,”destination_directory_path”) 

os.remove()

To remove a directory and all files and directories in it:

os.remove(“my_file_path”)

Recursive Looping

To loop over all files recursively under some path (i.e. including subdirectories) as follows:

import os

path = ‘/tmp/’

for root, directories, filenames in os.walk(path):
   for directory in directories:

       directory_path = os.path.join(root, directory)
    
   for filename in filenames:

       file_path = os.path.join(root,filename)

Read more about file systems here

 

Share
Picture of Ram Sagar

Ram Sagar

I have a master's degree in Robotics and I write about machine learning advancements.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.