python - Writing Another Text File based on Saved File from Tkinter -
i prompt user save file, , want automatically save file along it. example, let's user saves text file under program called "foo.txt", want automatically make text file called "lengthfoo.txt". word "length" before "foo", , ".txt" after "foo". example: when user saves "helloworld.txt", want program automatically save file called "lengthhelloworld.txt"
from ast import literal_eval tkinter import * tkfiledialog import * def loadfile(): try: save_file = askopenfile(mode='r', defaultextension=".zw") lengths_file = open("length%s.zw" % "origsavefilename", "r") save_file f: line in f: rectlist.extend(literal_eval(line.strip())) save_file.close() lengths_file.close() print "file loaded" def savefile(): try: save_file = asksaveasfile(mode='w', defaultextension=".zw") if save_file not none: # write file disk lengths_file = open("length%s.zw" % "origsavefilename", "w") save_file.close() print "file saved successfully."
don't use asksaveasfile
, use asksaveasfilename
, returns chosen file name instead of opened file.
you can create file name length
using like
import os save_filename = asksaveasfilename() save_filename_split = os.path.split(save_filename) save_filename_length = os.path.join(save_filename_split[0], 'length'+save_filename_split[-1])
then have 2 file names can open , write stuff to.
Comments
Post a Comment