File manager

How to make a Simple File Manager with Python

Simple Python File Manager

Ever wanted to know how a File manager works? How it runs? How the things are implemented? You are a beginner programmer? You are a python programmer?

If your answer to all of the above questions is “Yes”, then you are at the right place. This article will just show you the basic working of a file manager. Plus, it is just command line program and will work only on windows though. Working with file paths is more complex on other OS, so it is made only compatible for Windows. We will make a working Simple Python File Manager. 📁

Requirements:

  • Basic knowledge of python (including the knowledge of data file handling in python will work better)
  • A Windows system
  • Python 3.x/2.x installed in it. (3.x and 2.x has not much difference, but I will write the codes for both the versions and I leave the task for finding the difference to you). You can download python from the download page of python.org.
  • Basic knowledge about files and paths in a system.

So, here we go with the code…. ✈

(Every function proceeds with a comment to understand the usage of the function)
(Most of the features will be seen when you will run the program in the console mod rather than on python interpreter)

Python File manager

Python 3.x version


import shutil         #Contains functions for operating files
import os         #imports the os

def Read():        #For reading files
    path=input("Enter the location of file to read:")
    file=open(path,"r")
     print(file.read()) 
     input('Press Enter...')  
     file.close() 


def Write():    #For writing or creating files
    path=input("Enter the location of file to write or create:")
    if os.path.isfile(path):
        print('Rebuilding Existing file') #For existing file
    else:
        print('Creating new file') #For new file
    text=input("Enter the text to write:")
    file=open(path,"w")
    file.write(text)

def Add():      # Adding text to a file
    path=input("Enter the file location:")
    text=input("Enter the text to write:")
    file=open(path,"a")
    file.write('\n'+text)


def Delete():          #Deleting a File
    path=input("Enter the location of file to be write or create:")
    if os.path.exists(path):      # checks if the file exists
        print('File Found')     #For existing file
        os.remove(path)          #os.remove(file path) is used to delete
        print('File has been deleted')
    else:
        print('File Does\'nt exist')    #Is no file exist



def Dirlist():      #Listing files in a directory
    path=input("Enter the Directory location to list:")
    sortlist=sorted(os.listdir(path))       #Sorting and listing files
    i=0
    while(i<len(sortlist)):
        print(sortlist[i]+'\n')
        i+=1


def Check():       #Checking file or directory presence
    fp=int(input('Check the presence of \n1.File \n2.Directry \n'))
    if fp==1:
        path=input("Enter the file location:")
        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 location:")
        os.path.isdir(path)
        if os.path.isdir(path)==False:
            print('Directory Found')
        else:
            print('Directory Not Found')



def Move():        #For moving or renameing file
    path1=input('Enter the location of File to move or rename:')
    mr=int(input('1.Rename \n2.Move \n'))
    if mr==1:
        path2=input('Enter the resulting location with resulting file name:')
        shutil.move(path1,path2)
        print('File renamed')
    if mr==2:
        path2=input('Enter the location to move:')
        shutil.move(path1,path2)
        print('File moved')


def Copy():       #For copying
    path1=input('Enter the location of File to copy or rename:')
    path2=input('Enter the location to copy:')
    shutil.copy(path1,path2)
    print('File copied')


def Makedir():            #For creating directory
    path=input("Enter the directory name with location to make \neg. C:\\Hello\\Newdir \nWhere newdir is new directory:")
    os.makedirs(path) 
    print('Directory Created')


def Removedir():             #For removing Directory
    path=input('Enter the location 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 location of Program:')
    try:
        os.startfile(path)
    except:
        print('File not found')


run=1
while(run==1):     #Running the program again
    os.system('cls')        #Used to clear the screen after running again the program
    print('LiteKnight File Manager')
    dec=int(input('''1.Read a file
2.Write in a File
3.Append text in 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
Choose the option number:
'''))
    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.Run again \n2.Exit \nChoose the option number: \n"))
    if run==2:
        exit()

 

For Python 2.x version refer below:


import shutil         #Contains functions for operating files
import os         #imports the os

def Read():        #For reading files
    path=raw_input("Enter the location of file to read:")
    file=open(path,"r")
    print(file.read())
    input('Press Enter...')
    file.close()


def Write():    #For writing or creating files
    path=raw_input("Enter the location of file to write or create:")
    if os.path.isfile(path):
        print('Rebuilding Existing file') #For existing file
    else:
        print('Creating new file') #For new file
    text=raw_input("Enter the text to write:")
    file=open(path,"w")
    file.write(text)
    


def Add():      # Adding text to a file
    path=raw_input("Enter the file location:")
    text=raw_input("Enter the text to write:")
    file=open(path,"a")
    file.write('\n'+text)


def Delete():          #Deleting a File
    path=raw_input("Enter the location of file to be write or create:")
    if os.path.exists(path):      # checks if the file exists
        print('File Found')     #For existing file
        os.remove(path)          #os.remove(file path) is used to delete
        print('File has been deleted')
    else:
        print('File Does\'nt exist')    #Is no file exist


def Dirlist():      #Listing files in a directory
    path=raw_input("Enter the Directory location to list:")
    sortlist=sorted(os.listdir(path))       #Sorting and listing files
    i=0
    while(i<len(sortlist)):
        print(sortlist[i]+'\n')
        i+=1


def Check():       #Checking file or directory presence
    fp=input('Check the presence of \n1.File \n2.Directry \n')
    if fp==1:
        path=raw_input("Enter the file location:")
        os.path.isfile(path)
        if os.path.isfile(path)==True:
            print('File Found')
        else:
            print('File not Found')
    if fp==2:
        path=raw_input("Enter the Directory location:")
        os.path.isdir(path)
        if os.path.isdir(path)==False:
            print('Directory Found')
        else:
            print('Directory Not Found')


def Move():        #For moving or renameing file
    path1=raw_input('Enter the location of File to move or rename:')
    mr=input('1.Rename \n2.Move \n')
    if mr==1:
        path2=input('Enter the resulting location with resulting file name:')
        shutil.move(path1,path2)
        print('File renamed')
    if mr==2:
        path2=raw_input('Enter the location to move:')
        shutil.move(path1,path2)
        print('File moved')


def Copy():       #For copying
    path1=raw_input('Enter the location of File to copy or rename:')
    path2=raw_input('Enter the location to copy:')
    shutil.copy(path1,path2)
    print('File copied')


def Makedir():            #For creating directory
    path=raw_input("Enter the directory name with location to make \neg. C:\\Hello\\Newdir \nWhere newdir is new directory:")
    os.makedirs(path) 
    print('Directory Created')


def Removedir():             #For removing Directory
    path=raw_input('Enter the location of Directory:')
    treedir=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=raw_input('Enter the location of Program:')
    try:
        os.startfile(path)
    except:
        print('File not found')


run=1
while(run==1):     #Running the program again
    os.system('cls')        #Used to clear the screen after running again the program
    dec=input('''1.Read a file
2.Write in a File
3.Append text in 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
Choose the option number:
''')
    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=input("1.Run again \n2.Exit \nChoose the option number: \n")
    if run==2:
    exit()

I hope you will understand the functions and the statements easily. And this “Simple Python File Manager” is just a small function part of huge file managers. I just made this program for a better understanding about the file managements in a system.

I hope you gained something from this article.

Please share and like our post.  🙏

Leave a Reply