My vscode is having problems using the open() function to open a file.
I am trying to open a .txt file. Both the py file and .txt file are in the same folder (named "practice"), but python terminal is erroring out with
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('ex_text.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'ex_text.txt'
PS C:\Users\liamt\OneDrive\Desktop\VSCode> python -
I tried using the relative path to the .txt file but a permission error came up.
Traceback (most recent call last):
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice'
PS C:\Users\liamt\OneDrive\Desktop\VSCode>
What am I doing wrong? Really appreciate any help.
When using vscode, open the entire folder/directory containing all the files by using file > open folder, and not just double clicking the python file alone. By opening the entire folder, it is included in the workspace and can be opened by the python file that exists along with it.
I am writing a short program where I want to write an array full of street names to a .txt file. Everyting works fine, but if I convert the python code to a .exe with pyinstaller, I cant write to a new .txt file anymore. Why does this happen?
with open(f"C:\\Users\\auser\\Desktop\\tset\\{txtname}.txt", "a") as txt_file:
for line in new_addresses:
txt_file.write(line + "\n")
txt_file.close()
this is how I create the .exe:
pyinstaller --onefile streets.py
Traceback (most recent call last):
File "streets.py", line 65, in
PermissionError: [Errno 13] Permission denied: 'C:\Users\auser\Desktop\tset\test.txt'
[7048] Failed to execute script rewriteword
I got this to work now. My anti virus program was blocking for some reason the execution.
I'm trying to access the Gmail API via Python. Here is the instruction.
I have copied the credentials.json (I have also renamed a copy of it and called it token.json and added it into the main folder) and the quickstart.py files in the main folder. I run the quickstart.py in the cmd and using Atom IDE, but I get:
C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access token.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'credentials.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\HP Pavilion\Documents\Atom\Gmail\quickstart.py", line 34, in <module>
main()
File "C:\Users\HP Pavilion\Documents\Atom\Gmail\quickstart.py", line 18, in main
flow = client.flow_from_clientsecrets("credentials.json", SCOPES)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\client.py", line 2135, in flow_from_clientsecrets
cache=cache)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
return _loadfile(filename)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'credentials.json', 'No such file or directory', 2)
Seems like the .json files can't be found. They are however in the same folders. I have also tried to run the code as an administrator but without any success.
I am not sure what happened to you but I run the script with either PyCharm or VS Code and it runs perfectly.
The only prerequisite (probably you skipped this step) is that you should first generate the credentials.json file, download it and put it into the same directory where you have the python script. In addition, I created an empty tokens.json file.
First, it will automatically open your default browser to indicate which Gmail account you want to specify. Next, it will output the labels of your Gmail account.
My mistake was that I right clicked on my JSON file, selected rename, then named it credentials.JSON, which made the real file name credentials.JSON.JSON.
If it is in the same location as the script, you can try:
import os
path = os.path.abspath('credentials.json')
with open(path,'r'):
//code
I had the same issue and it turned out that the problem was with the IDE I'm using (Mu 1.1.0). Once I downloaded the JSON file, renamed it, placed it in the directory to where the error was pointing to, and ran everything with cmd, it worked as it should.
python 2.7. Notepad++. Windows 10
I'm trying to open file like this:
sourcefile=raw_input('name of the file: ')
fhand=open(sourcefile,'r')
And it gives me this error:
Traceback (most recent call last):
File "C:\Users\Denis\Desktop\prog\python\es.py", line 135, in <module>
fhand=open(sourcefile)
IOError: [Errno 2] No such file or directory: '20.04.2016.csv'
In notepad++ i run this: C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)"
Kinda new to programming but i'm guesing that I have to specify somehow path to the place where my es.py and my file is. I thought $(FULL_CURRENT_PATH) does it.
If i run my code with python or idle it works fine.
I've tried to run this program in Atom, gives same mistake. No mistakes so long as I don't work with files.
Pyinstaller & shutil [function copy] problem !
hi I'm trying to convert a .py code into .exe
actually my code is similar to that :
dr = os.environ['WINDIR']
current = os.path.abspath(__file__)
shutil.copy(current, dr)
as we can see the code copy the current fine to windows file (this is just an example ) ; the code actually works fine but if we compile it into exe and run it we will see .
Traceback (most recent call last):
File "<string>", line 13, in <module>
File "C:\Documents and Settings\john\Desktop\pyinstaller\pyinstaller-2.0\test
\build\pyi.win32\test\out00-PYZ.pyz\shutil", line 119, in copy
File "C:\Documents and Settings\john\Desktop\pyinstaller\pyinstaller-2.0\test
\build\pyi.win32\test\out00-PYZ.pyz\shutil", line 82, in copyfile
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\john
\\Desktop\\pyinstaller\\pyinstaller-2.0\\test\\dist\\test.py'
the error is from function copy because it doesn't find test.py in \test\dist\test.py .
any solutions ?
i tried :
dr = os.environ['WINDIR']
current = os.path.abspath(__file__)
newfile = current.replace('py','exe')
shutil.copy(current, dr)
but it still say
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\john
\\Desktop\\exeinstaller\\exeinstaller-2.0\\test\\dist\\test.exe'
It seems that when compiling into exe file os.path.abspath(__file__) copies the file location before compiling and is not treated as a variable.