Difference between revisions of "Python-for-Automation/C2/File-Management/English"
(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. | ||
− | |||
{| border="1" | {| border="1" | ||
|- | |- | ||
Line 7: | Line 11: | ||
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
− | |- | + | |- |
|| Show slide: | || Show slide: | ||
'''Welcome''' | '''Welcome''' | ||
|| Welcome to the Spoken Tutorial on '''" File Management ".''' | || Welcome to the Spoken Tutorial on '''" File Management ".''' | ||
− | |- | + | |- |
|| Show slide: | || Show slide: | ||
'''Learning Objectives''' | '''Learning Objectives''' | ||
|| In this tutorial, we will learn | || 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: | || Show slide: | ||
− | '''System Requirements'''* | + | '''System Requirements''' |
− | * | + | |
+ | * '''Ubuntu''' '''Linux OS 22.04''' | ||
+ | * '''Python 3.12.3''' | ||
|| To record this tutorial, I am using | || To record this tutorial, I am using | ||
− | * | + | * '''Ubuntu Linux os version 22.04''' |
− | * | + | * '''Python version 3.12.3''' |
− | |- | + | |- |
|| 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. |
− | |- | + | |- |
|| Show slide: | || Show slide: | ||
'''Code files''' | '''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: | || Show slide: | ||
'''File Management''' | '''File Management''' | ||
|| File management in automation is crucial: | || 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 ''' | || Show slide: '''File Management - Libraries ''' | ||
|| To automate file management, we need the below 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: | || Only narration: | ||
|| Let us see how this automation is done using python. | || Let us see how this automation is done using python. | ||
− | |- | + | |- |
|| Show '''Sample''' folder. | || Show '''Sample''' folder. | ||
− | || For this download the '''sample.zip''' file from the ''' | + | || For this download the '''sample.zip''' file from the '''Code files'''. |
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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'''. |
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. |
− | |- | + | |
+ | 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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 ''' | + | 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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.''' | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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'''.''' | ||
− | |- | + | |- |
|| 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. |
− | + | If not, we create it using the '''os.makedirs '''function'''. | |
− | + | |- | |
− | If not, we create it using the '''os.makedirs '''function'''. | + | |
− | |- | + | |
|| 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.''' | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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'''. | ||
− | |- | + | |- |
|| Only narration | || Only narration | ||
|| Now let us execute the code. | || Now let us execute the code. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| '''>'''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. | ||
− | |- | + | |- |
− | || '''>'''Type | + | || '''>'''Type '''cd Downloads''' |
− | || | + | || Now type, '''cd space Downloads'''. |
− | + | Press '''Enter.''' | |
− | |- | + | |- |
− | || '''>'''Type | + | || '''>'''Type '''python3 File_Management.py''' |
|| Let us run the code. | || Let us run the code. | ||
Line 278: | Line 288: | ||
Press '''Enter.''' | Press '''Enter.''' | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| Open the''' Sample folder''' | || Open the''' Sample folder''' | ||
− | || Let us open the '''Sample '''folder again | + | || Let us open the '''Sample '''folder again. |
− | |- | + | 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
− | || '''>'''Type | + | || '''>'''Type '''python3 File_Manag ement.py''' |
− | || | + | || 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.''' | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| >Type: '''deactivate''' | || >Type: '''deactivate''' | ||
|| Type '''deactivate '''to exit the virtual environment. | || Type '''deactivate '''to exit the virtual environment. | ||
− | |- | + | |- |
|| Show slide: | || Show slide: | ||
'''Summary''' | '''Summary''' | ||
− | || | + | || 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 | ||
− | * | + | * How to manage files using '''Python.''' |
− | * | + | * List the type of files in a directory. |
− | * | + | * Error and exception handling. |
− | |- | + | |- |
|| Show slide: | || Show slide: | ||
'''Assignment''' | '''Assignment''' | ||
|| As an assignment, please do the following: | || 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: | || Show slide: | ||
'''About the Spoken Tutorial Project''' | '''About the Spoken Tutorial Project''' | ||
− | || | + | || The video at the following link summarizes the '''Spoken Tutorial project'''. |
Please download and watch it. | Please download and watch it. | ||
− | |- | + | |- |
|| Show Slide: | || Show Slide: | ||
− | + | '''Spoken Tutorial Workshops''' | |
− | || | + | || The '''Spoken Tutorial Project''' team conducts workshops and gives certificates. |
For more details, please write to us. | For more details, please write to us. | ||
− | |- | + | |- |
|| 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. | ||
− | |- | + | |- |
|| Show Slide: | || Show Slide: | ||
'''FOSSEE Forum''' | '''FOSSEE Forum''' | ||
− | || | + | || For any general or technical questions on '''Python for''' |
− | '''Automation''' | + | '''Automation''', visit the''' FOSSEE forum''' and post your question. |
− | |- | + | |- |
|| 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'''. | ||
− | |- | + | |- |
|| Show slide: | || Show slide: | ||
'''Thank You''' | '''Thank You''' | ||
− | || | + | || This is '''Dhaarani Pushpam S, '''a FOSSEE Semester long intern 2024, IIT Bombay signing off. |
Thanks for joining. | Thanks for joining. | ||
|- | |- | ||
|} | |} | ||
− |
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
|
Show slide:
System Requirements
|
To record this tutorial, I am using
|
Show slide:
Prerequisite https://spoken-tutorial.org |
To follow this tutorial
|
Show slide:
Code files |
|
Show slide:
File Management |
File management in automation is crucial:
|
Show slide: File Management - Libraries | To automate file management, we need the below libraries:
|
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
|
Show slide:
Assignment |
As an assignment, please do the following:
|
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. |