ANSWERED! THANKS EVERYONE!
I'm trying to get my script to run, however I'm getting this error. I'm not brilliant with Python, so any form of basic explanation would be appreciated.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Brad>c:\python27\python.exe c:\python27\vsauce.py
ERROR:root:Error opening settings.json.
Traceback (most recent call last):
File "c:\python27\vsauce.py", line 76, in loadSettings
settingsFile = open("settings.json", "r")
IOError: [Errno 2] No such file or directory: 'settings.json'
The line in question
settingsFile = open("settings.json", "r")
Yes, the file exists. Yes, it is named "settings.json" exactly. I can't for the life of me figure out what the hell I have done wrong. This is not my script by the way.
Thanks in advance for any help!
try
$ cd C:\Python27
$ python.exe vsauce.py
this will move you to Python27 directory. so it will look there for settings.json
since you ran it from C:\Users\Brad it was looking for C:\Users\Brad\settings.json
The reason your script isn't working is because you're running it from C:\Users\Brad, so when
settingsFile = open("settings.json", "r")
runs, it looks for settings.json in the directory the script was run from, not the directory the script is stored in. You either need to hardcode the path the settings.json in your .py file, or you need to run the script from the C:\Python27 directory. To do this, after starting cmd.exe, enter
cd c:\Python27
python vsauce.py
Try opening while writing out the full directory. It may be that your current working directory is not the same as where the file is. By default when you use "open" python looks for files wherever you ran the program from. Such as 'C:\Users\Brad... whatever'. You need to change the directory if the file is saved elsewhere
settingsFile = open('C:\\Users\\Brad\\Documents\\PythonFiles\\YourDirectory\\settings.json', 'r')
Related
hello.py is my first python program. It is saved on my desktop.
In the terminal I write in front of
user#AA-MacBook-Air ~ % python3 hello.py
The error is
can't open file 'hello.py': [Errno 2] No such file or directory
Kindly help me understand the problem and solve it.
In the terminal you are currently in the directory ~. This signifies the folder /Users/<username>. Your script is on your desktop.
Type cd Desktop to change to /Users/<username>/Desktop and then run python3 hello.py.
you first need to change destination with cd
The error message, No such file or directory pretty much gives the explanation. Check if the file hello.py is present in the correct working directory. This can done graphically or using the ls command. If it is not present, copy the file the to the directory or navigate to the location of the file hello.py in terminal using cd.
I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)
I want to use some text files that I have in the same directory in my script. But Atom always gives me this error
FileNotFoundError: [Errno 2] No such file or directory: 'comments.txt'
[Finished in 0.156s]
Anyone know how to fix this?
This is my code
with open('comments.txt', 'r') as f:
myNames = [line.strip() for line in f]
print(myNames)
It works on other IDE's such as Pycharm and sublime text. I also tried it on the python idle.
In Atom, it only works when I give it the full path. But I want it to only use the project path.
If you are using the Script package, go to File>Settings (or CTRL+,) then open the Packages tab. Find script and open its Settings menu. The first setting is "Change Current Working Directory (CWD) Behavior." Change the value from "First project directory" to "Directory of script."
It could be that atom changes a file path.
Run a file or command to check
import os
print(os.getcwd())
This will tell you which dir you are working from.
Use this in your script to change dir
os.chdir(PATH_TO_DIR)
or write full file path
In atom, os.getcwd() always returns D:\WorkSpace\Test. So if I do things like open("01.txt"), it cannot find the file.
Also, this happens when using the "script" package to execute in Atom, But when executing the actual python file, it works.
I have found several other asking the same question, like this, but there is still no resolution.
Thanks to everyone who tried to help!
Added my directory:
D:\WorkSpace\Test
D:\WorkSpace\Test\01\01.py
D:\WorkSpace\Test\01\01.txt
or
D:\WorkSpace\Test
└─01
└─ 01.py
└─ 01.txt
Added my source:
01.py
import os
print os.getcwd()
f = open("01.txt")
print f.read()
01.txt
atom editor 01.txt
Added results(in atom):
D:\WorkSpace\Test
Traceback (most recent call last):
File "D:\WorkSpace\Test\01\01.py", line 5, in <module>
f = open("01.txt")
IOError: [Errno 2] No such file or directory: '01.txt'
Added results(in windows cmd):
D:\WorkSpace\Test\01>01.py
D:\WorkSpace\Test\01
atom editor 01.txt
In Mac, I solved this kind of issue by opening ATOM from a shell window, under target directory. Seems ATOM will use the directory it inherited from shell process as its working directory. You might try with a windows cmd window, see if it works.
Windows - go to Packages -> Settings View -> Manage Packages. Then go to System Settings on the left hand side menu and tick 'Show in file context menus'.
You can now go to your chosen directory and open any file (.js, .py etc.) with Atom and the current working directory will change to the one you chose instead of the default .atom.
I have some problem with my WIndows CMD.
Some time I need to open python file using CMD command. And I write: 'C:\Program Files\Python X.X\python.exe file.py' but have error: 'C:\Program' isn't system command (maybe not the same, I have another OS language).
With different methods I have different errors but can't open python file.
Examples:
(Picture) translate: can't find 'C:\Program'...
(Picture) another example when I trying to write python directory first and then start python file, but it can't find python file.
Thanks for helping me.
There seems to be 2 different problems here.
Windows does not recognise spaces in directory or file names on the command line, so you need to put the directory insied "" .
i.e. "C:\Program Files\Python 3.4\python.exe"
In your second picture, suggests that run.py does not exist in the current directory. Change Directory to where the run.py file is before running that command.
First of all go to the directory where your python file is located ... like:
cd "c:\users\someone\documents\..."
On your pictures you are trying to run python file located in system32 folder but i guess it is not located there so move where the file is with that cd command
Then as Martin says the problem with path of python.exe is the space between words. To solve put the path into quotation marks.
But u can add python to system path and insted of writing full path u can write only
python file.py
How to add python to path see here https://superuser.com/questions/143119/how-to-add-python-to-the-windows-path