Vsc is giving an error but the python idle doesent? - python

I wrote a program, and when I run it in Visual Studio Code it gives an error but not when I run it in the Python IDLE. I have set up the environment variable but it still doesn't work. So can u please tell me how to fix this
This also has happened when I import a file and all sorts of places where I want to use a different file
This is my compiler.py file
fileName = "file.txt"
file = open("file.txt", "r+")
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
for loop in range(file_len(fileName) + 1) :
print(loop)
and this is my file.txt
hallo
When I run this in Visual Studio Code it gives this error
PS C:\Users\Harry Kruger\Documents\code> & "C:/Program Files (x86)/Python37-32/python.exe" "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py"
hallo
Traceback (most recent call last):
File "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py", line 4, in <module>
file = open("file.txt", "r+")
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'
And when i run ir in python IDLE it works and the output is this
0
1

I guess that you run python IDLE in the directory "c:/Users/Harry Kruger/Documents/code/quicks. Therefore your code will pass because in this directory is (probably?) also the file.txt.
However in VS Code you seem to run python in the directory C:\Users\Harry Kruger\Documents\code where the file.txt is not present and therefore your code fails.
To fix this and run your code in VS Code you have two options:
In VS Code powershell navigate to the directory that contains the file.txt. In your case you should be able to do it by entering cd "c:/Users/Harry Kruger/Documents/code/quicks" and then call your code.
You can modify your code to use the absolute path of the file. Then you can invoke it from any directory. To do so you have to modify the with open() statement. Replace it with:
from os import path
with open(path.join(path.abspath(path.dirname(__file__)), 'file.txt'), 'r+') as f:
this snipet will look for the absoulte path of the file and open it.

Related

Why is the Current Working Directory when accessing a text file not the Directory where my program is saved?

I'm quite new to Python and am studying through a book. I'm just trying to run filereader.py which just opens and reads a text file. This text file is inside the same folder as the python code. but when I run it, this error appears:
Error:
Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory: 'pi_digits.txt'
File "C:\Users\lcsan\Desktop\python_work\Chapter 10\filereader.py", line 1, in <module>
with open('pi_digits.txt') as file_object:
Here's the code:
Filereader.py:
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
I tried importing os and using the os.getcwd() function to see where the program is searching for the text file.
This is the original Dir: (censored some parts)
C:\Users\*****\Desktop\python_work\Chapter 10'
This is the result of the os.getcwd() function:
C:\Users\*****\Desktop\python_work'
It seems that it is one directory behind the intended directory. Is there no other way to access it through relative path or do I really have to indicate the absolute path through os.chdir()?
The current directory will be whatever the current directory was when you executed the script. Running a Python script does not change the directory. If you need to access files in the same directory as the script, you can do that:
scriptpath = os.path.abspath(os.path.dirname( __file__ ))
with open(scriptpath + '/pi_digits.txt') as file_object:
I am not sure but if you are running code by terminal your terminals working directory should be same with codes directory.
if code's path dir1/dir2/code.py
files in directory:
code.py s
sample.txt
sample.txt content
text
text
text
text
this is a sample text
code:
import os
print(os.getcwd())
lines = open("sample.txt")
print(lines.read())
when ran from top directory:
C:\Users\User\Documents\dir1
Traceback (most recent call last):
File "dir2/code.py", line 3, in <module>
lines = open("sample.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'sample.txt'
when ran from same directory:
C:\Users\User\Documents\dir1\dir2>python code.py
C:\Users\User\Documents\dir1\dir2
text
text
text
text
this is a sample text

Pyinstaller create a data file at runtime

I created a .exe file with Pyinstaller. The program should create a data file with the results at runtime. It works in the Python interpreter mode when I execute python test.py but it doesn't work in the .exe mode. It simply doesn't create a file and responds with an error.
Which flags should I add to the command python -m PyInstaller --hidden-import=timeit --hidden-import=bisect -F test.py to make it work?
The exception with this setup is:
Error: [Errno 2] No such file or directory: "C:\Users\Admin\AppData\Local\Temp\..."
Where the aforementioned directory is temporary and I don't have access to it.
The piece of code which is supposed to write a file is:
def write_file(data, path, encoding='utf-8'):
'''Creates report files.'''
try:
if config.python_version == 2 and type(data) in (list, str):
f = io.open(path, 'wb')
else:
f = io.open(path, 'w', encoding=encoding, newline='')
if type(data) is list:
writer = csv.writer(f)
writer.writerows(data)
else:
f.write(data)
f.close()
console(u'Report file: ' + path)
except IOError as e:
console(e, level=Level.error)
I assume there should be a setting to point to the place where the file should be saved.
I've checked here https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-files-to-the-bundle but without success. I couldn't use the listed flags properly, nothing seemed to work.
How can I specify the place where the file would be saved?
The issue isn't with pyinstaller, it's with however you're creating your file.
You may be using some environment variable when running your python script from the command line that isn't set when you run your Exe
I have created a simple example of a program that creates a data file in the directory from which it is called:
#myscript.py
f = open("Test.txt", "w")
print("Hello World!", file=f)
Then I generate the Exe with Pyinstaller:
pyinstaller -F myscript.py
Copy the exe anywhere and you can create Test.txt, if you have permissions in that folder.
Double click myscript.exe
Test.txt appears in the same folder as myscript.exe
Hello World!

FileNotFoundError: [Errno 2] - Possible work network issue?

I am trying to read content of a file on my work network from my work network. I copy and pasted a code snippet from a google search and modified it to the below. Why might I still be getting [Errno 2] (I have changed some of the path names for this question board)
The file path in my file explorer shows that "> This PC > word common common" and I don't have "This PC" in my path. I tried adding that into the place I would think it goes in the string. That didn't solve it.
I tried making sure I have matching capitalization. That didn't solve it.
I tried renaming the file to have a *.txt on the end. That didn't solve it.
I tried the different variations of // and / and \ with and without the r predecessor and while that did eliminate the first error I was getting. It didn't help this error.
(Looking at the code errors in the right gutter is says my line length is greater than the PEP8 standard. While I doubt that is the root of my problem, if you can throw in the 'right' wrap method for a file path that long that would be helpful.)
myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt") # open lorem.txt for reading text
contents = myfile.read() # read the entire file into a string
myfile.close() # close the file
print(contents) # print contents
Full Error Copy:
C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.py", line 1, in
myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt") # open lorem.txt for reading text
FileNotFoundError: [Errno 2] No such file or directory: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'
EDIT
DEBUG EFFORTS
working to figure out how to change directory. Just in case that is the problem. Tested this code bit
import os
path = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)
Received this error
C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py", line 5, in <module>
os.chdir(path)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'
My intention for adding the picture below is to show how File Explorer displays the file path for my file
FileExplorerPathScreenShot
EDIT
I think this confirms that my 'OS' doesn't have my file.
from os import path
path.exists("PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246")
def main():
print ("File exists:" + str(path.exists('PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246')))
if __name__== "__main__":
main()
Output
File exists: False
I thought OS was a standard variable for Operating system. Now I'm not sure.
EDIT
Using Cmd in DOS, I confirmed that my path for the z: is correct
EDIT - Success
I ran
import os
print( os.listdir("z:/"))
Confirmed I don't need the monster string of folders.
Confirmed, although explorer doesn't show it, it is a *.txt file
Once I implemented these two items the first code worked fine.
Thank you #Furas
To open and read a file specify the filename in your path:
myfile = open("U:/matrix_neo/word common common/hello world.txt", "rt") # open file
contents = myfile.read() # read the entire file into a string
myfile.close() # close the file
print(contents) # print contents
The U: is a mapped drive in my network.
I did not find any issue with your change dir example. I used a path on my U: path again and it returned True.
import os
path = "U:/matrix_neo/word common common"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)
The check the attributes on the directory that you are trying to read from. Also try to copy the file to a local drive for a test and see if you can read the file and also check if it exists.
This is an alternative to the above and uses your path to make sure that the long file path works:
import os
mypath = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
myfile = 'whatever is your filename.txt'
if not os.path.isdir(mypath):
os.makedirs (mypath)
file_path = os.path.join(mypath, myfile)
print(file_path)
if os.path.exists(file_path) is True:
with open(file_path) as filein:
contents = filein.read()
print(contents)
I tested this code using a long csv file.,Replace the variable myfile with whatever is your file name.

(Error 2) No such file or directory in Python 3

I'm trying to display text from a file but am getting the mentioned error. I've tried changing the default directory using os.chdir(). I'm also getting the error even if I save the text file in the default directory. I'm using version 3.2.
Code:
from sys import argv
import os
script, filename = argv
txt = open(filename)
print(filename, "is the filename.")
print("Here is the text.", txt.read())
filename_again = input("Filename again pls.")
txt_again = open(filename_again)
print("Test again.", txt_again.read())
Error Report:
PS C:\Python32\Stuff\Programs> python Sample1.py fubar1.txt
Traceback (most recent call last):
File "Sample1.py", line 6, in <module>
txt = open(filename)
IOError: [Errno 2] No such file or directory: 'fubar1.txt'
PS C:\Python32\Stuff\Programs>
Tested the code and it worked for me. Tried to enter this as a comment, but don't have the points.
Tested on a Mac OS X Yosemite 10.10.2 using python 3.4.2
May I suggest it may have been a typo when entering the original filename? I tried different file names and it worked successfully. If this is not the case then debugging time.
Note Error Line
The error is encountered in the first open statement so it is possible that the arguments text needs to be trimmed.
Debugging Idea
Noticed you are in windows. Try using a print() command on line 5 to examine the contents of the filename variable. You may have spaces before or after the filename which are then causing the open statement to fail.

Opening a file in pwdir's folder in Python through Applescript

Opening a file available in present working directory's temp folder in python
I tried
pwdir=os.getcwd()
tempdir=pwdir+"/temp/test.txt"
f=open(tempdir,'r+')
When I print the path of tempdir, it is showing up correctly and also the contents of file are also read.
When I try to combine this operation from an Applescript, which calls this python script. I get an error like this
f=open(pwdir1,'r+')
IOError: [Errno 2] No such file or directory: '//temp/test.txt'" number 1
EDIT:
I am using Shell script from Applescript to call this pythonscript
do shell script "/Users/mymac/Documents/'Microsoft User Data'/test.py"
EDIT:
Python Code:
tempdir = os.path.join(os.getcwd(),'temp','htmlinput.html')
print tempdir
with open(tempdir) as f:
html=f.read()
Python output from terminal:(works perfectly fine)
/Users/mymac/Documents/Microsoft User Data/Outlook Script Menu Items/temp/htmlinput.html
I am also able to see the file contents.
Applescript Code:
do shell script "/Users/mymac/Documents/'Microsoft User Data'/'Outlook Script Menu Items'/new.py"
Applescript Error:
error "Microsoft Outlook got an error: Traceback (most recent call last):
File \"/Users/mymac/Documents/Microsoft User Data/Outlook Script Menu Items/new.py\", line 12, in <module>
with open(tempdir) as f:
IOError: [Errno 2] No such file or directory: '/temp/htmlinput.html'" number 1
I don't know Applescript -- or OS X in general. It looks like the script is being run from the root folder, and os.getcwd() returns '/'. The directory of the script itself is sys.path[0] or the dirname of the current module -- dirname(__file__) -- if it's a single script instead of a package. Try one of the following
import os, sys
tempdir = os.path.join(sys.path[0], 'temp', 'temp.txt')
or
import os
tempdir = os.path.join(os.path.dirname(__file__), 'temp', 'temp.txt')
The double slash is your problem. The right way to join file and path names in Python is with os.path.join. Try:
tempdir = os.path.join(os.getcwd(), 'temp', 'test.txt')
Also, you should probably be doing:
with open(tempdir) as f:
which will make sure tempdir gets closed even if you have an error.
Edit:
We need to see what tempdir is when the script is invoked by the AppleScript, not when it is invoked from the terminal. If you do
tempdir = os.path.join(os.getcwd(),'temp','htmlinput.html')
with open('/Users/mymac/Documents/temp.txt', 'w') as fwrite:
fwrite.write(tempdir)
What exactly ends up in the file /Users/mymac/Documents/temp.txt?

Categories

Resources