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...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
Title of the script: File Management
 +
 +
Author: Dhaarani Pushpam S
 +
 +
Keywords: python file management, directory handling, error handling, shutil library, os library, video tutorial, spoken tutorial.
  
  
<div style="margin-left:1.27cm;margin-right:0cm;"></div>
 
 
{| border="1"
 
{| border="1"
 
|-
 
|-
Line 7: Line 11:
 
|| '''Narration'''
 
|| '''Narration'''
 
|-
 
|-
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
 
'''Welcome'''
 
'''Welcome'''
 
|| Welcome to the Spoken Tutorial on '''" File Management ".'''
 
|| Welcome to the Spoken Tutorial on '''" File Management ".'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn  
 
|| In this tutorial, we will learn  
* <div style="margin-left:1.27cm;margin-right:0cm;">How to manage files using '''Python.'''</div>
+
* How to manage files using '''Python.'''
* <div style="margin-left:1.27cm;margin-right:0cm;">List the type of files in a directory.</div>
+
* List the type of files in a directory.
* <div style="margin-left:1.27cm;margin-right:0cm;">Error and exception handling.</div>
+
* Error and exception handling.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
'''System Requirements'''* <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu''' '''Linux OS 22.04'''</div>
+
'''System Requirements'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Python 3.12.3'''</div>
+
 
 +
* '''Ubuntu''' '''Linux OS 22.04'''
 +
* '''Python 3.12.3'''
  
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu Linux os version 22.04'''</div>
+
* '''Ubuntu Linux os version 22.04'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Python version 3.12.3'''</div>
+
* '''Python version 3.12.3'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
Line 40: Line 46:
 
* You must have basic knowledge of using '''Linux Terminal '''and''' Python.'''
 
* You must have basic knowledge of using '''Linux Terminal '''and''' Python.'''
 
* For pre-requisite '''Linux''' and '''Python '''Tutorials, please visit this website.
 
* For pre-requisite '''Linux''' and '''Python '''Tutorials, please visit this website.
* Python libraries required for automation must be installed
+
* Python libraries required for automation must be installed.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:
 
|| Show slide:
  
 
'''Code files'''
 
'''Code files'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">The files used in this tutorial are provided in the '''Code files''' link.</div>
+
* The files used in this tutorial are provided in the '''Code files''' link.
* <div style="margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="margin-left:1.27cm;margin-right:0cm;">Make a copy and then use them while practicing.</div>
+
* Make a copy and then use them while practicing.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
 
'''File Management'''
 
'''File Management'''
 
|| File management in automation is crucial:
 
|| File management in automation is crucial:
* <div style="margin-left:1.27cm;margin-right:0cm;">For seamless organization of files.</div>
+
* For seamless organization of files.
* <div style="margin-left:1.27cm;margin-right:0cm;">To ensure efficient storage and retrieval.</div>
+
* To ensure efficient storage and retrieval.
* <div style="margin-left:1.27cm;margin-right:0cm;">To facilitate smooth automated workflows.</div>
+
* To facilitate smooth automated workflows.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide: '''File Management - Libraries '''
 
|| Show slide: '''File Management - Libraries '''
 
|| To automate file management, we need the below libraries:
 
|| To automate file management, we need the below libraries:
* <div style="margin-left:1.27cm;margin-right:0cm;">'''shutil:''' It offers high-level operations on files and collections of files.</div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;">'''os:''' It provides functions for interacting with the operating system.</div>
 
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
 
|| <div style="color:#000000;">Show screenshot:(slides folder)</div>
 
  
<div style="color:#000000;">'''Sample_files.png'''</div>
+
* '''shutil:''' It offers high-level operations on files and collections of files.
|| <div style="color:#000000;">In this tutorial, we will learn how to organize files based on extension.</div>
+
  
<div style="color:#000000;">As you can see, this folder is disorganized with various files. </div>
+
* '''os:''' It provides functions for interacting with the operating system.
 +
|-
 +
|| Show screenshot:(slides folder)
  
<div style="color:#000000;">It is quite hard to search a file if your folder is unorganized.</div>
+
'''Sample_files.png'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| In this tutorial, we will learn how to organize files based on extension.
|| <div style="color:#000000;">Show screenshot:</div>
+
  
<div style="color:#000000;">'''Organized_files.png'''</div>
+
As you can see, this folder is disorganized with various files.  
|| <div style="color:#000000;">By using file automation, we can neatly organize files as shown here.</div>
+
  
<div style="color:#000000;">The files are sorted into folders by their extensions.</div>
+
It is quite hard to search a file if your folder is unorganized.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 +
|| 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:
 
|| Only narration:
 
|| Let us see how this automation is done using python.
 
|| Let us see how this automation is done using python.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show '''Sample''' folder.
 
|| Show '''Sample''' folder.
|| For this download the '''sample.zip''' file from the '''code files'''.
+
|| For this download the '''sample.zip''' file from the '''Code files'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Extract the zip file.
 
|| Extract the zip file.
 
|| Extract the files from the zip folder before using it.
 
|| Extract the files from the zip folder before using it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open '''Sample '''folder.
 
|| Open '''Sample '''folder.
|| The '''Sample '''folder contains various files and also a sub folder named '''Miscellaneous.'''
+
|| The '''Sample '''folder contains various files and also a sub folder named '''Miscellaneous'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open '''More files''' folder.
 
|| Open '''More files''' folder.
 
|| Inside the '''Miscellaneous''' folder, there are few more files.
 
|| Inside the '''Miscellaneous''' folder, there are few more files.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Point to the '''File Management.py '''in downloads folder
 
|| Point to the '''File Management.py '''in downloads folder
 
|| I have created a source code '''File Management.py '''for an automation demonstration.
 
|| I have created a source code '''File Management.py '''for an automation demonstration.
  
 
Let us open the file in the text editor.
 
Let us open the file in the text editor.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 108: Line 116:
 
'''import shutil'''
 
'''import shutil'''
 
|| First we import the '''os '''and '''shutil '''modules required for file management.
 
|| First we import the '''os '''and '''shutil '''modules required for file management.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:  
 
|| Highlight:  
  
 
'''def print_unique_extensions(directory):'''
 
'''def print_unique_extensions(directory):'''
|| Next, we define a function''' print_unique_extensions.'''
+
|| Next, we define a function''' print_unique_extensions'''.
  
 
This''' '''function prints all unique file extensions in a given directory.
 
This''' '''function prints all unique file extensions in a given directory.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:''' if not os.path.exists(directory):'''
 
|| Highlight:''' if not os.path.exists(directory):'''
  
Line 122: Line 130:
 
'''return'''
 
'''return'''
  
|| we check if the given directory exists.If not, we print the directory not found and exit the function.
+
|| we check if the given directory exists.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
 +
If not, we print the directory not found and exit the function.
 +
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''try:'''
 
'''try:'''
 
|| We then use a '''try '''block to catch potential errors.
 
|| We then use a '''try '''block to catch potential errors.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''extensions = set()'''
 
'''extensions = set()'''
 
|| Inside the block we create an empty set to store unique extensions.
 
|| Inside the block we create an empty set to store unique extensions.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 145: Line 155:
 
The '''underscore''' represents the list of directories.  
 
The '''underscore''' represents the list of directories.  
  
Since it is not used in the '''os.walk''' function, let us keep it as '''_.'''
+
Since it is not used in the '''os.walk''' function, let us keep it as '''underscore(_)'''.
  
 
The '''os.walk '''function generates file names in a directory tree.
 
The '''os.walk '''function generates file names in a directory tree.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''for file in files:'''
 
'''for file in files:'''
 
|| Then we use a '''nested''' '''for '''loop to iterate through each file.
 
|| Then we use a '''nested''' '''for '''loop to iterate through each file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 161: Line 171:
 
|| Inside the loop the '''os.path.splitext '''function gets the extension from each file.
 
|| 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.
+
We use the '''extensions.add '''function to add the extension to the set.
  
 
Then we print all the extensions.
 
Then we print all the extensions.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 171: Line 181:
 
'''print(f"Error listing files: {e}")'''
 
'''print(f"Error listing files: {e}")'''
 
|| If an error occurs, the '''except '''block will catch the exception and print a message.
 
|| If an error occurs, the '''except '''block will catch the exception and print a message.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 178: Line 188:
  
 
This''' '''function organizes files by their extensions within the parent directories.
 
This''' '''function organizes files by their extensions within the parent directories.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 185: Line 195:
  
 
The '''os.walk''' is used to traverse the directory tree.
 
The '''os.walk''' is used to traverse the directory tree.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:'''extension_dirs = {}'''
 
|| Highlight:'''extension_dirs = {}'''
 
|| We then initialize a dictionary to store directories for each extension.
 
|| We then initialize a dictionary to store directories for each extension.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 197: Line 207:
  
 
Then using '''os.path.join '''we construct the full file path.
 
Then using '''os.path.join '''we construct the full file path.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:'''if os.path.isfile(os.path.join(directory, file)):'''
 
|| Highlight:'''if os.path.isfile(os.path.join(directory, file)):'''
 
|| Now we use an '''if '''condition to check if it's actually a 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.'''
 
To do this we use the function '''os.path.isfile.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight: '''_, extension = os.path.splitext(file)'''
 
|| Highlight: '''_, extension = os.path.splitext(file)'''
 
|| Using '''os.path.splitext '''we split the base name and the extension.
 
|| Using '''os.path.splitext '''we split the base name and the extension.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 211: Line 221:
 
|| We then use a '''nested if '''to check if the file has an extension.
 
|| We then use a '''nested if '''to check if the file has an extension.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''target_dir = os.path.join(directory, extension[1:])'''
 
'''target_dir = os.path.join(directory, extension[1:])'''
 
|| Next we create a directory name based on the extension using '''os.path.join''' function'''.'''
 
|| Next we create a directory name based on the extension using '''os.path.join''' function'''.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:  
 
|| Highlight:  
  
 
'''if target_dir not in extension_dirs: os.makedirs(target_dir, exist_ok=True) '''
 
'''if target_dir not in extension_dirs: os.makedirs(target_dir, exist_ok=True) '''
||  
+
|| Next using a '''nested''' '''if '''we check if the target directory already exists.
  
By using a '''nested''' '''if '''we check if the target directory already exists.
+
If not, we create it using the '''os.makedirs '''function'''.
 
+
|-  
If not, we create it using the '''os.makedirs '''function'''.'''
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
|| Highlight: '''shutil.move(os.path.join(directory, file), os.path.join(target_dir, file))'''
 
|| 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.'''
 
|| We then move the file to the target directory using '''shutil.move.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 236: Line 244:
 
'''print(f"Error organizing files: {e}")'''
 
'''print(f"Error organizing files: {e}")'''
 
|| If an error occurs, the '''except '''block will catch the exception and print a message.
 
|| If an error occurs, the '''except '''block will catch the exception and print a message.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 244: Line 252:
  
 
The '''directory_path '''holds the path to the directory you want to organize.
 
The '''directory_path '''holds the path to the directory you want to organize.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight: '''print_unique_extensions(directory_path)'''
 
|| Highlight: '''print_unique_extensions(directory_path)'''
  
 
'''organize_files_by_extension(directory_path)'''
 
'''organize_files_by_extension(directory_path)'''
 
|| We then call the two functions '''print_unique_extensions''' and '''organize_files_by_extension'''.
 
|| We then call the two functions '''print_unique_extensions''' and '''organize_files_by_extension'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration
 
|| Only narration
 
|| Now let us execute the code.
 
|| Now let us execute the code.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration
 
|| Only narration
 
|| Save the code as '''File Management.py '''in the '''Downloads '''folder.
 
|| Save the code as '''File Management.py '''in the '''Downloads '''folder.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open terminal'''(Ctrl + Alt + T)'''
 
|| Open terminal'''(Ctrl + Alt + T)'''
 
|| Open the '''terminal''' by pressing '''Control + Alt + T '''keys simultaneously.
 
|| Open the '''terminal''' by pressing '''Control + Alt + T '''keys simultaneously.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''>'''Type '''source Automation/bin/activate'''
 
|| '''>'''Type '''source Automation/bin/activate'''
  
 
Then press enter.
 
Then press enter.
|| We’ll open the virtual environment for the '''Automation''' series.Type '''source space Automation forward slash bin forward slash activate.'''
+
|| We’ll open the virtual environment for the '''Automation''' series.
 +
 
 +
Type '''source space Automation forward slash bin forward slash activate.'''
  
 
Then press enter.
 
Then press enter.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''>'''Type <span style="background-color:#ffffff;">'''cd Downloads'''</span>
+
|| '''>'''Type '''cd Downloads'''
|| <span style="background-color:#ffffff;">Now type, </span><span style="background-color:#ffffff;">'''cd Downloads'''</span><span style="background-color:#ffffff;">.</span>
+
|| Now type, '''cd space Downloads'''.
  
<span style="background-color:#ffffff;">Press </span><span style="background-color:#ffffff;">'''Enter.'''</span>
+
Press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''>'''Type <span style="background-color:#ffffff;">'''python3 File_Management.py'''</span>
+
|| '''>'''Type '''python3 File_Management.py'''
 
|| Let us run the code.
 
|| Let us run the code.
  
Line 278: Line 288:
  
 
Press '''Enter.'''
 
Press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:'''Enter the directory path to organize: '''
 
|| Highlight:'''Enter the directory path to organize: '''
  
Line 285: Line 295:
 
Type the location of your folder as shown here.
 
Type the location of your folder as shown here.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight the output:
 
|| Highlight the output:
  
 
'''Unique file extensions: {'.mov', '.txt', '.pdf', '.mp4', '.doc', '.csv', '.ppt', '.png', '.webm', '.mp3', '.Identifier'}'''
 
'''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.
 
|| We can see that the unique extensions in the '''sample folder''' are listed here.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight :
 
|| Highlight :
  
 
'''The Files are organized Successfully.'''
 
'''The Files are organized Successfully.'''
 
|| Also we can see a message saying the files are organized successfully.
 
|| Also we can see a message saying the files are organized successfully.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open the''' Sample folder'''
 
|| Open the''' Sample folder'''
|| Let us open the '''Sample '''folder again.We can see that files are organized in directories based on their extensions.
+
|| Let us open the '''Sample '''folder again.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
We can see that files are organized in directories based on their extensions.
 +
 
 +
|-  
 
|| Open the '''Miscellaneous folder'''
 
|| Open the '''Miscellaneous folder'''
 
|| Let us open the '''Miscellaneous folder '''too.
 
|| Let us open the '''Miscellaneous folder '''too.
  
 
The files inside the sub folder are also now organized.
 
The files inside the sub folder are also now organized.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration:
 
|| Only narration:
 
|| Let us now see the error handling while executing the code.
 
|| Let us now see the error handling while executing the code.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''>'''Type <span style="background-color:#ffffff;">'''python3 File_Manag ement.py'''</span>
+
|| '''>'''Type '''python3 File_Manag ement.py'''
|| <div style="color:#ff0000;"></div>
+
|| In the terminal, type '''python3 space File underscore Management dot py '''
 
+
In the terminal, type '''python3 space File underscore Management dot py '''
+
  
 
Press '''Enter.'''
 
Press '''Enter.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:'''Enter the directory path to organize: '''
 
|| Highlight:'''Enter the directory path to organize: '''
  
 
>Type''' test'''
 
>Type''' test'''
 
|| Type '''test '''The directory''' test '''does not exist in my system.
 
|| Type '''test '''The directory''' test '''does not exist in my system.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:'''Error: Directory 'test' not found.'''
 
|| Highlight:'''Error: Directory 'test' not found.'''
 
|| We can see that the output shows an error as the directory is not found.
 
|| We can see that the output shows an error as the directory is not found.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration
 
|| Only narration
  
Line 330: Line 340:
 
The '''try - except '''block in the source code will catch these errors.
 
The '''try - except '''block in the source code will catch these errors.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| >Type: '''deactivate'''
 
|| >Type: '''deactivate'''
 
|| Type '''deactivate '''to exit the virtual environment.
 
|| Type '''deactivate '''to exit the virtual environment.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
 
'''Summary'''
 
'''Summary'''
|| <span style="background-color:#ffffff;">This brings us to the end of the tutorial. Let us summarize.</span>
+
|| This brings us to the end of the tutorial.  
 +
 
 +
Let us summarize.
  
 
In this tutorial, we have learnt about
 
In this tutorial, we have learnt about
* <div style="margin-left:1.27cm;margin-right:0cm;">How to manage files using '''Python.'''</div>
+
* How to manage files using '''Python.'''
* <div style="margin-left:1.27cm;margin-right:0cm;">List the type of files in a directory.</div>
+
* List the type of files in a directory.
* <div style="margin-left:1.27cm;margin-right:0cm;">Error and exception handling.</div>
+
* Error and exception handling.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
 
'''Assignment'''
 
'''Assignment'''
 
|| As an assignment, please do the following:
 
|| As an assignment, please do the following:
* <div style="margin-left:1.27cm;margin-right:0cm;">Organize files based on extension.</div>
+
* Organize files based on extension.
* <div style="margin-left:1.27cm;margin-right:0cm;">Print the number of directories created.</div>
+
* Print the number of directories created.
* <div style="margin-left:1.27cm;margin-right:0cm;">Print the number of files in each directory.</div>
+
* Print the number of files in each directory.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:  
 
|| Show slide:  
  
 
'''About the Spoken Tutorial Project'''
 
'''About the Spoken Tutorial Project'''
|| <span style="background-color:#ffffff;">The video at the following link summarizes the </span><span style="background-color:#ffffff;">'''Spoken Tutorial project'''</span><span style="background-color:#ffffff;">.</span>
+
|| The video at the following link summarizes the '''Spoken Tutorial project'''.
  
 
Please download and watch it.
 
Please download and watch it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show Slide:
 
|| Show Slide:
  
<span style="background-color:#ffffff;">'''Spoken Tutorial Workshops'''</span>
+
'''Spoken Tutorial Workshops'''  
|| <span style="background-color:#ffffff;">The </span><span style="background-color:#ffffff;">'''Spoken Tutorial Project'''</span><span style="background-color:#ffffff;"> team conducts workshops and gives certificates.</span>
+
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
  
 
For more details, please write to us.
 
For more details, please write to us.
|- style="border:0.75pt solid #808080;padding:0.176cm;"
+
|-  
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Answers for THIS Spoken Tutorial'''
 
'''Answers for THIS Spoken Tutorial'''
 
|| Please post your timed queries in this forum.
 
|| Please post your timed queries in this forum.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show Slide:
 
|| Show Slide:
  
 
'''FOSSEE Forum'''
 
'''FOSSEE Forum'''
|| <span style="background-color:#ffffff;">For any general or technical questions on </span>'''Python for'''
+
|| For any general or technical questions on '''Python for'''
  
'''Automation'''<span style="background-color:#ffffff;">, visit the</span><span style="background-color:#ffffff;">''' FOSSEE forum'''</span><span style="background-color:#ffffff;"> and post your question.</span>
+
'''Automation''', visit the''' FOSSEE forum''' and post your question.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide
 
|| Show slide
  
 
'''Acknowledgement'''
 
'''Acknowledgement'''
 
|| '''Spoken Tutorial''' Project was established by the '''Ministry of Education, Government of India'''.
 
|| '''Spoken Tutorial''' Project was established by the '''Ministry of Education, Government of India'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show slide:
 
|| Show slide:
  
 
'''Thank You'''
 
'''Thank You'''
|| <span style="background-color:#ffffff;">This is </span><span style="background-color:#ffffff;">'''Dhaarani Pushpam S, '''</span><span style="background-color:#ffffff;">a FOSSEE Semester long intern 2024, IIT Bombay signing off.</span>
+
|| This is '''Dhaarani Pushpam S, '''a FOSSEE Semester long intern 2024, IIT Bombay signing off.
  
 
Thanks for joining.
 
Thanks for joining.
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:0cm;margin-right:0cm;"></div>
 

Latest revision as of 11:58, 26 August 2024

Title of the script: File Management

Author: Dhaarani Pushpam S

Keywords: python file management, directory handling, error handling, shutil library, os library, video tutorial, spoken tutorial.


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 underscore(_).

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 extensions.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)

Next 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 space 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