-->

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’s
number 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.

  1. import shutil
  2. import os
  3. import time
  4. import subprocess
  5. def Read():
  6.     path=input(“Enter the file path to read:”)
  7.     file=open(path,”r”)
  8.     print(file.read())
  9.     input(‘Press Enter...’)
  10.     file.close()
  11. def Write():
  12.     path=input(“Enter the path of file to write or create:”)
  13.     if os.path.isfile(path):
  14.         print(‘Rebuilding the existing file’)
  15.     else:
  16.         print(‘Creating the new file’)
  17.     text=input(“Enter text:”)
  18.     file=open(path,”w”)
  19.     file.write(text)
  20. def Add():
  21.     path=input(“Enter the file path:”)
  22.     text=input(“Enter the text to add:”)
  23.     file=open(path,”a”)
  24.     file.write(‘\n’+text)
  25. def Delete():
  26.     path=input(“Enter the path of file for deletion:”)
  27.     if os.path.exists(path):
  28.         print(‘File Found’)
  29.         os.remove(path)
  30.         print(‘File has been deleted’)
  31.     else:
  32.         print(‘File Does not exist’)
  33. def Dirlist():
  34.     path=input(“Enter the Directory path to display:”)
  35.     sortlist=sorted(os.listdir(path))
  36.     i=0
  37.     while(i<len(sortlist)):
  38.     print(sortlist[i]+’\n’)
  39.     i+=1
  40. def Check():
  41.     fp=int(input(‘Check existence of \n1.File \n2.
  42.     Directory\n’))
  43.     if fp==1:
  44.         path=input(“Enter the file path:”)
  45.         os.path.isfile(path)
  46.     if os.path.isfile(path)==True:
  47.         print(‘File Found’)
  48.     else:
  49.         print(‘File not found’)
  50.     if fp==2:
  51.         path=input(“Enter the directory path:”)
  52.         os.path.isdir(path)
  53.     if os.path.isdir(path)==False:
  54.         print(‘Directory Found’)
  55.     else:
  56.         print(‘Directory Not Found’)
  57. def Move():
  58.     path1=input(‘Enter the source path of file to move:’)
  59.     mr=int(input(‘1.Rename \n2.Move \n’))
  60.     if mr==1:
  61.         path2=input(‘Enter the destination path and file name:’)
  62.         shutil.move(path1,path2)
  63.         print(‘File renamed’)
  64.     if mr==2:
  65.         path2=input(‘Enter the path to move:’)
  66.         shutil.move(path1,path2)
  67.         print(‘File moved’)
  68. def Copy():
  69.     path1=input(‘Enter the path of the file to copy or rename:’)
  70.     path2=input(‘Enter the path to copy to:’)
  71.     shutil.copy(path1,path2)
  72.     print(‘File copied’)
  73. def Makedir():
  74.     path=input(“Enter the directory name with path to make
  75.     \neg. C:\\Hello\\Newdir \nWhere Newdir is new
  76.     directory:”)
  77.     os.makedirs(path)
  78.     print(‘Directory Created’)
  79. def Removedir():
  80.     path=input(‘Enter the path of Directory:’)
  81.     treedir=int(input(‘1.Deleted Directory \n2.Delete
  82.     Directory Tree \n3.Exit \n’))
  83.     if treedir==1:
  84.         os.rmdir(path)
  85.     if treedir==2:
  86.         shutil.rmtree(path)
  87.         print(‘Directory Deleted’)
  88.     if treedir==3:
  89.         exit()
  90. def Openfile():
  91.     path=input(‘Enter the path of program:’)
  92.     try:
  93.     os.startfile(path)
  94.     except:
  95.         print(‘File not found’)
  96.         run=1
  97.         while(run==1):
  98.     try:
  99.         os.system(‘clear’)
  100.         except OSError:
  101.         os.system(‘cls’)
  102.         print(‘\n>>>>>>>>>>Python 3 File Manager<<<<<<<<<<\n’)
  103.         print(‘The current time and date is:’,time.asctime())
  104.         print(‘\nChoose the option number: \n’)
  105. dec=int(input(‘’’1.Read a file
  106. 2.Write to a file
  107. 3.Append text to a file
  108. 4.Delete a file
  109. 5.List files in a directory
  110. 6.Check file existence
  111. 7.Move a file
  112. 8.Copy a file
  113. 9.Create a directory
  114. 10.Delete a directory
  115. 11.Open a program
  116. 12.Exit
  117. ‘’’))
  118.     if dec==1:
  119.         Read()
  120.     if dec==2:
  121.         Write()
  122.     if dec==3:
  123.         Add()
  124.     if dec==4:
  125.         Delete()
  126.     if dec==5:
  127.         Dirlist()
  128.     if dec==6:
  129.         Check()
  130.     if dec==7:
  131.         Move()
  132.     if dec==8:
  133.         Copy()
  134.     if dec==9:
  135.         Makedir()
  136.     if dec==10:
  137.         Removedir()
  138.     if dec==11:
  139.         Openfile()
  140.     if dec==12:
  141.         exit()
  142.         run=int(input(“1.Return to menu\n2.Exit \n”))
  143.     if run==2:
  144.         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.

1 Response to "Python File Manager"

  1. 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.
    Document Management Software
    Electronic Document Management System
    Document Management System
    Cloud Document Management Software

    ReplyDelete

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel