OpenFiles_3.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. def uploadTextFile(self):
  2. files = filedialog.askopenfilenames(
  3. initialdir=os.getcwd(),
  4. title="Choose Text files",
  5. filetypes=(("Text Files", "*.txt"),)
  6. )
  7. try:
  8. for tf in files:
  9. if tf == "":
  10. self.textUploadVar.set("")
  11. return
  12. tf = open(tf) # or tf = open(tf, 'r')
  13. # copy file over to tcl locations
  14. shutil.copy(tf.name, os.getcwd() + os.path.join("\\studentlists"))
  15. tf.close()
  16. except:
  17. self.studentListLabel.configure(style="Red.TLabel")
  18. self.textUploadVar.set("An Error Occurred")
  19. return
  20. if len(files) == 1:
  21. # Generate an animation for the result
  22. self.studentListLabel.configure(style="Green.TLabel")
  23. self.textUploadVar.set("Copied " + files[0][files[0].rfind("/") + 1:len(files[0])])
  24. elif len(files) > 1:
  25. self.studentListLabel.configure(style="Green.TLabel")
  26. self.textUploadVar.set("Copied " + str(len(files)) + " Files")
  27. elif len(files) == 0:
  28. self.studentListLabel.configure(style="TLabel")
  29. self.textUploadVar.set("")