Python - Check File Exists

Submitted by: 
Language: 
Visitors have accessed this post 204 times.

In this tutorial we will create a Check File Exists using Python. Python has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. So let's now do the coding...

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy code that I provided below and paste it inside the IDLE text editor.

  1. from tkinter import *
  2. import os
  3. from os import walk

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. #==================================MAIN FRAME=====================================
  2. root = Tk()
  3. root.title("Python - Check File Exists")
  4. width=450
  5. height=350
  6. screen_width=root.winfo_screenwidth()
  7. screen_height=root.winfo_screenheight()
  8. x=(screen_width/2) - (width/2)
  9. y=(screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Assigning Variables

This is where the we will assign the variables. This code will assign each of the variables to be use later in the application.

  1. #=================================VARIABLES=======================================
  2. FILES = StringVar()

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #=================================FRAMES==========================================
  2. Top = Frame(root, relief=SOLID, bd=1)
  3. Top.pack(fill=X)
  4. Mid = Frame(root)
  5. Mid.pack(pady=20)
  6. Left = Frame(Mid)
  7. Left.pack(side=LEFT)
  8. LeftPane = Frame(Left)
  9. LeftPane.pack(side=BOTTOM)
  10. Right = Frame(Mid)
  11. Right.pack(side=RIGHT, padx=20)
  12. RightPane = Frame(Right, relief=SOLID, bd=1)
  13. RightPane.pack(side=BOTTOM)
  14.  
  15. #=================================LABEL WIDGETS===================================
  16. lbl_title = Label(Top, text="Check File Exist", font=('calibri', 16))
  17. lbl_title.pack()
  18. lbl_file = Label(Left, text="File", font=('calibri', 16))
  19. lbl_file.pack(side=LEFT)
  20. lbl_txt = Label(Right, text="List Of Files", font=('calibri', 16))
  21. lbl_txt.pack(side=TOP)
  22. lbl_result = Label(LeftPane)
  23. lbl_result.pack()
  24.  
  25.  
  26. #=================================ENTRY WIDGETS===================================
  27. txt_file = Entry(Left, font=('calibri', 12), textvariable=FILES)
  28. txt_file.pack(side=RIGHT)
  29.  
  30.  
  31. #=================================BUTTON WIDGETS==================================
  32. btn_check = Button(LeftPane, text="Check", command=Check_File)
  33. btn_check.pack(pady=10)

Creating the Main Function

This is where the code that contains the main funcitions. This code will check if the file is already existed. To do that just copy and write these blocks of code.

  1. #=================================METHODS=========================================
  2.  
  3. def FileList():
  4. file = []
  5. for (path, dir, filename) in walk('./file'):
  6. file.extend(filename)
  7.  
  8. for i in range(len(file)):
  9. exec ('Label%d=Label(RightPane,text="%s")\nLabel%d.pack(side=BOTTOM)' % (i, file[i], i))
  10.  
  11.  
  12. def Check_File():
  13. if FILES.get() == "":
  14. lbl_result.config(text="Please Enter Something", fg="orange")
  15. else:
  16. if os.path.exists('./file/' + FILES.get()):
  17. lbl_result.config(text="File exist!", fg="Green")
  18. else:
  19. lbl_result.config(text="File not exist!", fg="Red")
  20.  
  21.  
  22. FileList()

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.

  1. #=================================INITIALIZATION==================================
  2. if __name__ == '__main__':
  3. root.mainloop()

There you have it we just created a Check File Exists Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.