Python can't find file - python

I am having great difficulty getting python 3.4 to recognize a path or text file on a windows 8 system. I have tried a variety of different approaches but get the similar errors (which likely implies something simple regarding the syntax).
The file itself is located in the same folder as the script file trying to open it:
C:\Users\User\Desktop\Python stuff\Data.txt
for simplicity, the simplest means to access the file (at least that I know of) is
f=open
These lines were coded as:
f = open("Data.txt", "r")
and
f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
but return the error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\Python stuff\Testscript.py", line 3, in <module>
f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/User/Desktop/Python stuff/Data.txt'

Please check your current directory.
For that just do this:
import os
print (os.getcwd())

I had this same issue. I was using VS Code for my IDE, and had the folder setting to a folder above where I had the file. This obviously was the issue. Once I opened the folder in VScode where the code and the text file were both located I was able to open the file with no issues.

When using Windows you want to keep in mind that if you name the file data.txt the file will actually be data.txt.txt, however Windows will show it as data.txt because windows hides the file extensions by default. This option can be changed by following the steps in the first comment.
If you then search data.txt there really is no such file.

for f = open("Data.txt", "r")
Make sure your .py and .txt files are in the same directory.
for f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
I think the space in Python stuff is messing things up.
Update: I just tried this out .. seems to be fine with the space actually.
>>> [i for i in open('/Users/pk-msrb/projects/temp/temp es/temp.txt')]
['1\n', '2\n', '3\n', '\n']

The problem might be, where exactly you are executing the file.
I had the same problem, but noticed, that the terminal in my IDE (VS Code) is executing the file in a different location. By simply cd to the files location in the terminal, everything went fine. ;)

I had the same problem but now it works for me. I used to open a big map with a lot of sub-maps and the file was in one of the sub-maps. Now I open the submap where my program and file.txt is located and the code open("file.txt", "r") works

I know this is an old question, but here is another option.
set your parent_folder if you have multiple files in the same folder:
parent folder = "C:/Users/User/Desktop/"
extend the folder if necessary:
import os.path
folder = os.path.join( parent_folder, "python stuff" )
add the filename:
file = os.path.join( folder, "Data.txt" )
open the file:
if os.path.exists( file ):
with open( file, "r" ) as infile:
data = infile.read()
or:
import os
import os.path
# get the desktop location ( since this is a special folder )
desktop = os.path.join(( os.environ["userprofile"] ), "desktop" )
file = os.path.join( desktop, "python stuff", "data.txt" )
if os.path.exists( file ):
with open( file, "r" ) as infile:
data = infile.read()

I had a similar issue when using vs code. If you use the built-in green triangle to run, it seems to be able to run the code but it doesn't know where to look for the file. open an integrated terminal to the correct folder and run from the terminal line. Seems like something vs code could figure out...

I had the same problem with pyCharm. I found out that you sometimes have to specify the directory by going to edit config and working directory

Try creating a file using:
f = open("myfile.txt", "x")
Then search your computer for "myfile.txt" which is the file you just created.
Wherever the location of the file is, that is where your code is reading from :)

Related

Need help to open a file in python

I don't know what's wrong here, all I'm trying to do is to open this file, but it says it can't find such a file or directory, however as I have highlighted on the side, the file is right there. I just want to open it. I have opened files before but never encountered this. I must have missed something, I checked online, and seems like my syntax is correct, but I don't know.
I get the same error when I try with "alphabetical_words" which is just a text file.
When open() function receives a relative path, it looks for that file relative to the current working directory. In other words: relative to the current directory from where the script is run. This can be any arbitrary location.
I guess what you want to do is look for alphabetical.csv file relative to the script location. To do that use the following formula:
from pathlib import Path
# Get directory of this script
THIS_DIR = Path(__file__).absolute().parent
# Get path of CSV file relative to the directory of this script
CSV_PATH = THIS_DIR.joinpath("alphabetical.csv")
# Open the CSV file using a with-block
with CSV_PATH.open(encoding="utf-8") as csvfile:
pass # Do stuff with opened file
You need to import csv. Then you can open the file as a csv file. For example,
with open ("alphabetical.csv", "r") as csvfile:
Then you can use the file.
Make sure the file is in your cwd.

Can't open CSV file even with full file path

I'm using Python 3.5 and I'm having some problems opening a CSV file. I've tried entering the entire path but it still doesn't work, but the file is clearly in the folder. (My code is called 'simplecsvtest.py')
Here's the code snippet:
import csv
import sys
file = open(r"C:\python35\files\results.csv", 'rt')
try:
reader = csv.reader(file, delimiter='\t')
... some code here ...
finally:
file.close()
And here's what PowerShell says:
PS C:\python35\files> python simplecsvtest.py
Traceback (most recent call last):
File "simplecsvtest.py", line 20, in
file = open(r"C:\python35\files\results.csv", 'rt')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\python35\\files\\results.csv'
Well, I'm very certain that 'results.csv' is in that folder: here's the filepath in Windows Explorer:
C:\Python35\files
(Note: The folder has capital 'P' for Python35, and I've tried having both capitalized and uncapitalized 'P' in the code, neither works)
The CSV file is a "Microsoft Excel Comma Separated Values Files", if that matters, but the extension is still csv. Can anyone tell me what's wrong?
I would suggest creating a folder inside your project folder and then use a relative path :
file = open(r".\files\results.csv", 'rt')
. implies that the path is relative to your current directory
I figured out a work-around solution myself:
Somehow, if I copy all the data from the csv and paste it in a new excel spreadsheet and save it as a csv, it works. I don't know why.
Although the file path is absolute it wouldn't work this way.
You have to use "forward" slashes in the path name instead of "backward" slashes. As
file = open(r"C:/python35/files/results.csv", 'rt')

Failing to open csv file

My current python script is like this:
import csv
with open ('2017_IL_sales.csv','r') as infile:
reader=csv.reader(infile)
with open('2017_IL_sales_report.csv') as outfile:
writer=csv.writer(outfile)
dict_report={rows[1]:rows[2] for rows in reader}
print dict_report
In brief, I want to open a csv file called 2017_IL_sales then create a dictionary for two columns inside. But with whatever reason, everytime I run the code via IDLE it told me this IOError: [Errno 2] No such file or directory: '2017_IL_sales.csv'. Anyone knows the reason?
Make sure that the script you are running is in the same folder as the file you are trying to read. If that is not possible, make sure to specify the correct file path.
Is IDLE's working directory that folder?
Run this in IDLE and see what you get:
import os
os.getcwd()

error "no such file or directory" when reading in csv file in python

Currently I am trying to read in a csv file using the csv module in python. When I run the piece of code below I get an error that states the file does not exist. My first guess is that maybe, I have the file saved in the wrong place or I need to provide pyton with a file path. currently I have the file saved in C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv.
one side note im note sure wether having set the mode to 'rb' (read binary) was the right move.
import csv
with open('test_satdata.csv', 'rb') as csvfile:
satreader = csv.reader(csvfile, delimiter=' ', lineterminator=" ")
for row in satreader:
print ', '.join(row)
Here is the errror code.
Traceback (most recent call last):
File "C:/Python27/test code/test csv parse.py", line 2, in <module>
with open('test_satdata.csv', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'test_satdata.csv'
Your code is using a relative path; python is looking in the current directory (whatever that may be) to load your file. What the current directory is depends on how you started your Python script and if you executed any code that may have changed the current working directory.
Use a full absolute path instead:
path = r'C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv'
with open(path, 'rb') as csvfile:
Using 'rb' is entirely correct, the csv module recommends you do so:
If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.
Windows is such a platform.
You can hit this error when you're running a Python script from a Directory where the file is not contained.
Sounds simple to fix, put the CSV file in the same folder as the .PY file. However when you're running under an IDE like VSCode the command output might cd to another directory when it executes your python file.
PS C:\git\awesome> cd 'c:\git\awesome'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1';
& 'C:\Program Files (x86)\Python37-32\python.exe' 'c:\Users\jeremy\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py'
'--default' '--client' '--host' 'localhost' '--port' '1089'
'c:\git\Project\ReadCSV.py'
See how I'm in my awesome repo and the first command is: cd 'c:\git\awesome';
Then it executes the python file: 'c:\git\Project\ReadCSV.py'
So its expecting the CSV file in 'c:\git\awesome'.
To fix it, either use the full file names or CD to the directory containing the CSV file you wish to read.
Your current guess is right: either put the file in your test code directory or point python to the right path.
Make a fresh rename of your folder. That worked for me.

How do I create a file at a specific path?

In python I´m creating a file doing:
f = open("test.py", "a")
where is the file created? How can I create a file on a specific path?
f = open("C:\Test.py", "a")
returns error.
The file path "c:\Test\blah" will have a tab character for the `\T'. You need to use either:
"C:\\Test"
or
r"C:\Test"
I recommend using the os module to avoid trouble in cross-platform. (windows,linux,mac)
Cause if the directory doesn't exists, it will return an exception.
import os
filepath = os.path.join('c:/your/full/path', 'filename')
if not os.path.exists('c:/your/full/path'):
os.makedirs('c:/your/full/path')
f = open(filepath, "a")
If this will be a function for a system or something, you can improve it by adding try/except for error control.
where is the file created?
In the application's current working directory. You can use os.getcwd to check it, and os.chdir to change it.
Opening file in the root directory probably fails due to lack of privileges.
It will be created once you close the file (with or without writing). Use os.path.join() to create your path eg
filepath = os.path.join("c:\\","test.py")
The file is created wherever the root of the python interpreter was started.
Eg, you start python in /home/user/program, then the file "test.py" would be located at /home/user/program/test.py
f = open("test.py", "a") Will be created in whatever directory the python file is run from.
I'm not sure about the other error...I don't work in windows.
The besty practice is to use '/' and a so called 'raw string' to define file path in Python.
path = r"C:/Test.py"
However, a normal program may not have the permission to write in the C: drive root directory. You may need to allow your program to do so, or choose something more reasonable since you probably not need to do so.
Use os module
filename = "random.txt"
x = os.path.join("path", "filename")
with open(x, "w") as file:
file.write("Your Text")
file.close

Categories