#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Question About Blank Copy For Writing To A File Project


    Hello,

    So I created a Python program that will prompt the user to specifically open up a .txt file that he wants to copy, and then he will be prompted to 'save as' for the new version of the text file that will be the exact same contents as the one he opened up, just with the word 'Copy' at the top.

    When I run this, and save it as 'Copy.txt' for example, it created the text file, but it has nothing in it.

    I do have a line that writes specifically to the copied version, so I'm having trouble with where I'm going wrong.

    Here's my code:


    Code:
    # Method write(str): Writes a string to a file (unlike PRINT, this does NOT
    # append an extra newline character
    
    # So inherently, in Python, Print is really
    # like PrintLine ins Java)
    
    # This write() function will be apparent when we actually output what was inside
    # the original text file
    
    # Task: Make a copy of a file, but put the word "Copy" in the first line of the
    # copied file.
    
    # Module tkinter: Continas types and functions for creating graphical user
    # interfaces
    
    # Module tkinter.filedialog: Contains functions for creating file dialogs
    
    import tkinter.filedialog
    
    # tkinter.filedialog.askopenfilename()
    
    # This is commented out because I want to set this equal to from_filename
    # later on
    
    # This will import the tkinter module that will contains gui options
    
    # We will then prompt the user to enter the filename of whatever he wants
    # to copy, which will obviously still be 'In Flanders Fields.txt'
    
    from_filename = tkinter.filedialog.askopenfilename()
    
    # This will open up a gui for the user to find the Flander's Fields open,
    # so that we can copy that directory within Windows in order to probably
    # store a copied version in it
    
    
    to_filename = tkinter.filedialog.asksaveasfilename()
    
    # This will prompt the user where he wants to save the new copy of the text file
    
    
    from_file = open(from_filename, 'r')
    
    # This will open the file the user first opened up for reading hence the use of
    # 'r'
    
    contents = from_file.read()
    
    # This is just a string variable that will store the entire contents of what's
    # inside the ORIGINAL Flander's Fields poem text file
    
    
    from_file.close()
    
    # This will then close the file to prevent memory leaks
    
    to_file = open(to_filename, 'w')
    
    # This will open the file that we want to write to, aka whatever we saved
    # as in the second step
    
    to_file.write("Copy\n")
    
    # This will write the word 'Copy' at the top of the file
    
    to_file.write(contents)
    
    # This will write the entire poem from Flander's Fields (or whatever we choose
    # really) into the new text file, so basically you're making a really primitive
    # form of Wordpad (pretty cool)
    
    to_file.close
    
    # This will just close the file
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    this program works fine, and is easier to read without the comments, in my opinion.
    Code:
    #Copy
    import tkinter.filedialog
    
    from_filename = tkinter.filedialog.askopenfilename()
    to_filename = tkinter.filedialog.asksaveasfilename()
    
    with open(from_filename, 'r') as from_file:
        content = from_file.read()
    
    with open(to_filename, 'w') as to_file:
        to_file.write('#Copy\n')
        to_file.write(content)
    The only substantial variation from your version is that you didn't actually call the to_file.close method. So I suppose depending on how you used your program and your operating system and when you looked for the copy might influence whether you thought your program worked. Prepending "Copy" to a file will destroy a lot of useful files. On the other hand, it's a handy for passing files through virus detection programs.

    to_file.close # no parenthesis to call the function.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    WOW! I just noticed I didn't put the parenthesis for the .copy() method, oh my God!

    Thank you so much, your version is a lot more refined heh , its just that I comment along as I do the videos for a free course in Python I found on the web

    Thanks a bunch!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    As a further side note, thank you so much b49P23TIvg for your help in my progress so far, you always have way less code and a more efficient solution for all the problems I've had thus far haha.

    Its pretty funny honestly, how many just outright stupid mistakes I've made for these projects, but at least I understand them.

    Once again, thanks man

IMN logo majestic logo threadwatch logo seochat tools logo