Python File Manager
Python Code Project Examples
To help you on your way to becoming a better coder, we’ve included some great Python code examples for you to type in, use and expand for your own programming projects. There’snumber guessing, random number and password generators, scrolling text examples, an on-screen digital clock, the beginning of a text adventure, a classic game of Hangman and much, much more for you to snip and use.
The only limitation here on is your own imagination but we’ve also added a handy glossary of terms in case you get stuck along the way. Wherever you go next in coding, whether it’s on to learning more languages, creating amazing apps and games or helping out and teaching new coders to avoid the pitfalls you made, the next steps are upon you.
"Keep on coding and keep on learning."
This file manager program displays a list of options that allow you to read a file, write to a file, append to a file, delete a file, list the contents of a directory and much more. It’s remarkably easy to edit and insert into your own code, or add to.
FILEMAN.PY
Copy the code below into a New > File and save it as FileMan.py. Once executed it will display the program title, along with the current time and date and the available options.
import shutil import os import time import subprocess def Read(): path=input(“Enter the file path to read:”) file=open(path,”r”) print(file.read()) input(‘Press Enter...’) file.close() def Write(): path=input(“Enter the path of file to write or create:”) if os.path.isfile(path): print(‘Rebuilding the existing file’) else: print(‘Creating the new file’) text=input(“Enter text:”) file=open(path,”w”) file.write(text) def Add(): path=input(“Enter the file path:”) text=input(“Enter the text to add:”) file=open(path,”a”) file.write(‘\n’+text) def Delete(): path=input(“Enter the path of file for deletion:”) if os.path.exists(path): print(‘File Found’) os.remove(path) print(‘File has been deleted’) else: print(‘File Does not exist’) def Dirlist(): path=input(“Enter the Directory path to display:”) sortlist=sorted(os.listdir(path)) i=0 while(i<len(sortlist)): print(sortlist[i]+’\n’) i+=1 def Check(): fp=int(input(‘Check existence of \n1.File \n2. Directory\n’)) if fp==1: path=input(“Enter the file path:”) os.path.isfile(path) if os.path.isfile(path)==True: print(‘File Found’) else: print(‘File not found’) if fp==2: path=input(“Enter the directory path:”) os.path.isdir(path) if os.path.isdir(path)==False: print(‘Directory Found’) else: print(‘Directory Not Found’) def Move(): path1=input(‘Enter the source path of file to move:’) mr=int(input(‘1.Rename \n2.Move \n’)) if mr==1: path2=input(‘Enter the destination path and file name:’) shutil.move(path1,path2) print(‘File renamed’) if mr==2: path2=input(‘Enter the path to move:’) shutil.move(path1,path2) print(‘File moved’) def Copy(): path1=input(‘Enter the path of the file to copy or rename:’) path2=input(‘Enter the path to copy to:’) shutil.copy(path1,path2) print(‘File copied’) def Makedir(): path=input(“Enter the directory name with path to make \neg. C:\\Hello\\Newdir \nWhere Newdir is new directory:”) os.makedirs(path) print(‘Directory Created’) def Removedir(): path=input(‘Enter the path of Directory:’) treedir=int(input(‘1.Deleted Directory \n2.Delete Directory Tree \n3.Exit \n’)) if treedir==1: os.rmdir(path) if treedir==2: shutil.rmtree(path) print(‘Directory Deleted’) if treedir==3: exit() def Openfile(): path=input(‘Enter the path of program:’) try: os.startfile(path) except: print(‘File not found’) run=1 while(run==1): try: os.system(‘clear’) except OSError: os.system(‘cls’) print(‘\n>>>>>>>>>>Python 3 File Manager<<<<<<<<<<\n’) print(‘The current time and date is:’,time.asctime()) print(‘\nChoose the option number: \n’) dec=int(input(‘’’1.Read a file 2.Write to a file 3.Append text to a file 4.Delete a file 5.List files in a directory 6.Check file existence 7.Move a file 8.Copy a file 9.Create a directory 10.Delete a directory 11.Open a program 12.Exit ‘’’)) if dec==1: Read() if dec==2: Write() if dec==3: Add() if dec==4: Delete() if dec==5: Dirlist() if dec==6: Check() if dec==7: Move() if dec==8: Copy() if dec==9: Makedir() if dec==10: Removedir() if dec==11: Openfile() if dec==12: exit() run=int(input(“1.Return to menu\n2.Exit \n”)) if run==2: exit()
Imports
There are three modules to import here: Shutil, OS and Time. The first two deal with the operating system and file management and manipulation; and the Time module simply displays the current time and date.Note how we’ve included a try and except block to check if the user is running the code on a Linux system or Windows. Windows uses CLS to clear the screen, while Linux uses clear. The try block should work well enough but it’s a point of possible improvement depending on your own system.
Hi admin, Congratulations I was looking for something like that and found it here. I'm really grateful for your blog post. You will find a lot of approaches after visiting your post.
ReplyDeleteDocument Management Software
Electronic Document Management System
Document Management System
Cloud Document Management Software