r/pythonhelp • u/[deleted] • Jul 29 '21
My Script Runs in Pycharm but Will Not Run in Python
I wrote a piece of code and I have been testing it in Pycharm and it works exactly as intended, but when I go to run it through regular Python, it doesn't complete the code.
The program allows for the user to name and save a file to their device, when I run it in Python after it asks for the file name it will automatically close out without saving the file. It is not supposed to do this. It is supposed to save the file and after asking the user to repeat the program, iterate through the code again or then close the program.
Every other feature of the code works but it will not save the file and just crashes. Why is this? Do I need to change the code or something? And if not, is there another way to run this script? I tried doing it through the terminal as well but made no progress.
I will leave the part of the code that deals with the file saving below;
Ask_User_To_Save_Dataframe = input("Do you want to save your Dataframe, above, as a file to this computer? Yes or No: ")if Ask_User_To_Save_Dataframe == str("yes") or Ask_User_To_Save_Dataframe == str("Yes"):User_Names_Save_File = input("What do you want to name the file of the above Dataframe?: ")f = open(User_Names_Save_File, "x+")Turn_Dataframe_To_String = Drop_All_Na_From_Dataframe.to_string()f.write(Turn_Dataframe_To_String)f.close()
1
u/MrPhungx Jul 29 '21
You said it just crashes. What is the error message? Maybe you need Admin priviliges to create files on your system. Try to start the terminal as admin (or use sudo to execute the script in an non Windows Environment)
1
u/chrisking206 Jul 29 '21
Using file mode x, open will fail if the file already exists, and you don’t for that before calling open. That said, posting the actual error would help us to help you further.
3
u/HeeebsInc Jul 29 '21
Without error I’m not sure but I’m guessing you just typed python instead of python3 if it worked in pycharm.
Run python3 script_name.py