Difference between revisions of "Python-for-Automation/C2/File-Management/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " <div style="margin-left:1.27cm;margin-right:0cm;"></div> {| border="1" |- || '''Visual Cue''' || '''Narration''' |- |- style="border:1pt solid #000000;padding-top:0cm;paddin...")
(No difference)

Revision as of 18:01, 22 August 2024


Visual Cue Narration
Show slide:

Welcome

Welcome to the Spoken Tutorial on " File Management ".
Show slide:

Learning Objectives

In this tutorial, we will learn
  • How to manage files using Python.
  • List the type of files in a directory.
  • Error and exception handling.
Show slide: System Requirements*
Ubuntu Linux OS 22.04
  • Python 3.12.3
To record this tutorial, I am using
  • Ubuntu Linux os version 22.04
  • Python version 3.12.3
Show slide:

Prerequisite https://spoken-tutorial.org

To follow this tutorial
  • You must have basic knowledge of using Linux Terminal and Python.
  • For pre-requisite Linux and Python Tutorials, please visit this website.
  • Python libraries required for automation must be installed
Show slide:

Code files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show slide:

File Management

File management in automation is crucial:
  • For seamless organization of files.
  • To ensure efficient storage and retrieval.
  • To facilitate smooth automated workflows.
Show slide: File Management - Libraries To automate file management, we need the below libraries:
  • shutil: It offers high-level operations on files and collections of files.
  • os: It provides functions for interacting with the operating system.
Show screenshot:(slides folder)
Sample_files.png
In this tutorial, we will learn how to organize files based on extension.
As you can see, this folder is disorganized with various files.
It is quite hard to search a file if your folder is unorganized.
Show screenshot:
Organized_files.png
By using file automation, we can neatly organize files as shown here.
The files are sorted into folders by their extensions.
Only narration: Let us see how this automation is done using python.
Show Sample folder. For this download the sample.zip file from the code files.
Extract the zip file. Extract the files from the zip folder before using it.
Open Sample folder. The Sample folder contains various files and also a sub folder named Miscellaneous.
Open More files folder. Inside the Miscellaneous folder, there are few more files.
Point to the File Management.py in downloads folder I have created a source code File Management.py for an automation demonstration.

Let us open the file in the text editor.

Highlight:

import os

import shutil

First we import the os and shutil modules required for file management.
Highlight:

def print_unique_extensions(directory):

Next, we define a function print_unique_extensions.

This function prints all unique file extensions in a given directory.

Highlight: if not os.path.exists(directory):

print(f"Error: Directory '{directory}' not found.")

return

we check if the given directory exists.If not, we print the directory not found and exit the function.
Highlight:

try:

We then use a try block to catch potential errors.
Highlight:

extensions = set()

Inside the block we create an empty set to store unique extensions.
Highlight:

for root, _, files in os.walk(directory):

Next we use a for loop to iterate through the directory.

Here,

The root is the current directory path.

The underscore represents the list of directories.

Since it is not used in the os.walk function, let us keep it as _.

The os.walk function generates file names in a directory tree.

Highlight:

for file in files:

Then we use a nested for loop to iterate through each file.
Highlight:

extensions.add(os.path.splitext(file)[1])

print("Unique file extensions:", extensions)

Inside the loop the os.path.splitext function gets the extension from each file.

We use the extension.add function to add the extension to the set.

Then we print all the extensions.

Highlight:

except Exception as e:

print(f"Error listing files: {e}")

If an error occurs, the except block will catch the exception and print a message.
Highlight:

def organize_files_by_extension(directory):

Now we define a function named organize_files_by_extension.

This function organizes files by their extensions within the parent directories.

Highlight:

try: for root, _, files in os.walk(directory):

Within a try block, We run a for loop to iterate the directory.

The os.walk is used to traverse the directory tree.

Highlight:extension_dirs = {} We then initialize a dictionary to store directories for each extension.
Highlight:

for file in files:

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

We then use a nested for loop to iterate through each file.

Then using os.path.join we construct the full file path.

Highlight:if os.path.isfile(os.path.join(directory, file)): Now we use an if condition to check if it's actually a file.

To do this we use the function os.path.isfile.

Highlight: _, extension = os.path.splitext(file) Using os.path.splitext we split the base name and the extension.
Highlight:

if extension:

We then use a nested if to check if the file has an extension.
Highlight:

target_dir = os.path.join(directory, extension[1:])

Next we create a directory name based on the extension using os.path.join function.
Highlight:

if target_dir not in extension_dirs: os.makedirs(target_dir, exist_ok=True)

By using a nested if we check if the target directory already exists.

If not, we create it using the os.makedirs function.

Highlight: shutil.move(os.path.join(directory, file), os.path.join(target_dir, file)) We then move the file to the target directory using shutil.move.
Highlight:

except Exception as e:

print(f"Error organizing files: {e}")

If an error occurs, the except block will catch the exception and print a message.
Highlight:

directory_path = input("Enter the directory path to organize: ")

Finally, we get the location of the directory from the user.

The directory_path holds the path to the directory you want to organize.

Highlight: print_unique_extensions(directory_path)

organize_files_by_extension(directory_path)

We then call the two functions print_unique_extensions and organize_files_by_extension.
Only narration Now let us execute the code.
Only narration Save the code as File Management.py in the Downloads folder.
Open terminal(Ctrl + Alt + T) Open the terminal by pressing Control + Alt + T keys simultaneously.
>Type source Automation/bin/activate

Then press enter.

We’ll open the virtual environment for the Automation series.Type source space Automation forward slash bin forward slash activate.

Then press enter.

>Type cd Downloads Now type, cd Downloads.

Press Enter.

>Type python3 File_Management.py Let us run the code.

Type python3 space File underscore Management dot py

Press Enter.

Highlight:Enter the directory path to organize:

>Type /home/dhaarani/Downloads/Sample

Here I want to organize the sample folder in my Downloads.

Type the location of your folder as shown here.

Highlight the output:

Unique file extensions: {'.mov', '.txt', '.pdf', '.mp4', '.doc', '.csv', '.ppt', '.png', '.webm', '.mp3', '.Identifier'}

We can see that the unique extensions in the sample folder are listed here.
Highlight :

The Files are organized Successfully.

Also we can see a message saying the files are organized successfully.
Open the Sample folder Let us open the Sample folder again.We can see that files are organized in directories based on their extensions.
Open the Miscellaneous folder Let us open the Miscellaneous folder too.

The files inside the sub folder are also now organized.

Only narration: Let us now see the error handling while executing the code.
>Type python3 File_Manag ement.py

In the terminal, type python3 space File underscore Management dot py

Press Enter.

Highlight:Enter the directory path to organize:

>Type test

Type test The directory test does not exist in my system.
Highlight:Error: Directory 'test' not found. We can see that the output shows an error as the directory is not found.
Only narration Similarly if the folder location does not have any files we get an error message.

The try - except block in the source code will catch these errors.

>Type: deactivate Type deactivate to exit the virtual environment.
Show slide:

Summary

This brings us to the end of the tutorial. Let us summarize.

In this tutorial, we have learnt about

  • How to manage files using Python.
  • List the type of files in a directory.
  • Error and exception handling.
Show slide:

Assignment

As an assignment, please do the following:
  • Organize files based on extension.
  • Print the number of directories created.
  • Print the number of files in each directory.
Show slide:

About the Spoken Tutorial Project

The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

Show Slide:

Spoken Tutorial Workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show Slide:

FOSSEE Forum

For any general or technical questions on Python for

Automation, visit the FOSSEE forum and post your question.

Show slide

Acknowledgement

Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Show slide:

Thank You

This is Dhaarani Pushpam S, a FOSSEE Semester long intern 2024, IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat