Austi
Austi

Reputation: 25

Try to save and write to a file

while True: # Saving a file in txt file
    print("Would you like to save the latest generation? ('y' to save): ")
    saveInput = input()
    if saveInput == 'y' or saveInput == 'Y':
        print("Enter destination file name: ")
        fileName = input() 

        try:
            open(fileName, "r")
            close(fileName)
            print("Do you want to overwrite that file? ('y' to continue): ")
            confirm = input()

            if confirm == 'n':
                print("Enter destination file name: ")
                confirm2 = input()
                open(confirm2, 'w')

            elif confirm == 'y':
                open(confirm, 'w')
                for line in new_glider:
                    confirm2.writelines(new_glider)

                print(new_glider)



        except:
               break 

This is what i got so far, I'm trying to first read a file take the data from that, and run it through my program and at the end ask if they want to save it, if the file exists ask if they want to overwrite it, and if not create a new one but when i try it skips after you input the destination name like so:

Output

Enter input file name: 
g.txt

How many new generations would you like to print? 
4
Would you like to save the latest generation? ('y' to save): 
y
Enter destination file name: 
g.txt
>>>

Can someone help me out? I've been stuck on it for a while

Upvotes: 2

Views: 99

Answers (2)

lovesmiles07
lovesmiles07

Reputation: 48

In the code part where you "try" to open the file, the file doesn't exist yet, so it gets to the "except" part (break) and the program terminates.

     try:
            open(fileName, "r")
            close(fileName)
            print("Do you want to overwrite that file? ('y' to continue): ")
            confirm = input()

            if confirm == 'n':
                print("Enter destination file name: ")
                confirm2 = input()
                open(confirm2, 'w')

            elif confirm == 'y':
                open(confirm, 'w')
                for line in new_glider:
                    confirm2.writelines(new_glider)

                print(new_glider)

        except:
               break 

Replace it with os.path.isfile(fileName)

if os.path.isfile(fileName):
    print("Do you want to overwrite that file? ('y' to continue): ")
    confirm = input()

    if confirm == 'n':
        print("Enter destination file name: ")
        confirm2 = input()
        open(confirm2, 'w')

    elif confirm == 'y':
        open(**fileName**, 'w')

        for line in new_glider:
            confirm2.writelines(new_glider)

        print(new_glider)

# if fileName doesn't exist, create a new file and write the line to it.
else:
    open(**fileName**, 'w')

    for line in new_glider:
       confirm2.writelines(new_glider)

    print(new_glider)

Upvotes: 1

DevShark
DevShark

Reputation: 9112

When you open the file, you need to create a variable to hold that file and write to it.

Right now, you are trying to call writelines on a string, not a file when you do this: confirm2.writelines(new_glider)

Here's how to write to a file properly:

with open(confirm, 'w') as f:
    f.writelines(new_glider)

Upvotes: 0

Related Questions