0

File management related doubt in Python3!

(IDE related, not playground!) Ok, so, I was trying to practice files management and got stuck here! This is the part of the code where I'm practicing it. The program closes without reading or writing the files. What am I doing wrong? ( ## THIS IS THE PART WHERE IT'S SUPPOSED TO READ THE FILES if txtmenuopt == "R": try: print("\nWhat file do wish to open?\n") openfilename = input(">") print("\nLoading " , openfilename , "\n") openfile = open('"' , openfilename , '"' , "r") print(openfile.read().splitlines()) openfile.close() input("Press enter to close file.\n\n") except: print("Uh-oh! Something wrong happened!\n") Full code at: https://code.sololearn.com/c0s7pmL79Upa/?ref=app

15th Jul 2020, 4:05 PM
Gabriel Luppi
Gabriel Luppi - avatar
4 odpowiedzi
+ 7
Please do not post codes in this length here direct in the post. Put it in playground an link it here. Where did you do your try: playground or IDE? Thanks!
15th Jul 2020, 4:10 PM
Lothar
Lothar - avatar
+ 3
There is an issue with the open() function, that creates the problem ( see error message as comment in the file). I have taken the code and reworked it slightly. Before you run this code, make sure that a txt file is in the same directory as the python file. This is a part of your code. All the rest is not tested from me. https://code.sololearn.com/cMGi1h12I1BA/?ref=app
16th Jul 2020, 2:36 PM
Lothar
Lothar - avatar
0
Your indentation is all over the place. Run the code, carefully read the exception, you'll immediately know what's wrong.
15th Jul 2020, 5:26 PM
Artem Khaiet
Artem Khaiet - avatar
0
I really don't get it. The code doesn't throw any exception! :( When I input the file: >>>test.txt The output is: >>>Loading test.txt >>>test.txt currently has the following: >>>Uh-oh! Something wrong happened! It seems to open the file correctly, since it wipes the data if use the "w" argument when opening it, however, it fails to print the content of the file or writing to it.
16th Jul 2020, 11:25 AM
Gabriel Luppi
Gabriel Luppi - avatar