This question already has answers here:
How to reliably open a file in the same directory as the currently running script
(9 answers)
Closed 2 years ago.
I am trying to open a file with python like this:
m = open("e.txt", 'r')
The text file I'm trying to open is in the same directory as my python file is.
However I'm getting an error message.
FileNotFoundError: [Errno 2] No such file or directory: 'e.txt'
I've also tried using:
import os
cwd = os.getcwd() # cwd: current working directory
path = os.path.join(cwd, "e.txt")
The error message looks a little different this time:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\e.txt'
I hope someone can help me with this issue. Thanks in advance.
The could be in a folder, if this is the case then the full name has to be written. Such as:
m = open("E://folder/e.txt","r")
print(m.read())
It is important to write the EXACT directory of the file.
The code is perfectly correct. Check the type of file whether it is .txt or some other file. If everything is correct you should check the location you gave
Related
This question already has an answer here:
Refering to a directory in a Flask app doesn't work unless the path is absolute
(1 answer)
Closed last year.
My application was working fine until yesterday - it's nearly finished and is due tonight. Yesterday for some reason it failed to recognise any of the files within the app folder (I am using VSC). I have no idea why this suddenly occurred, but I have checked the directory of the app.py file and it is the same path as my additional files. I even tried loading my project on a different pc incase the problem was with my internal file system, but got the same error.
The error printed below is for the first file my app calls, datab.yaml, but when I remove this I get the same error with the next file I try to call (sentiment_model.pkl), and so on.
Error:
File "c:\Users\jeram\Documents\NCI\Semester3\TriagatronX50\app.py", line 57, in <module>
db = yaml.safe_load(open('datab.yaml'))
FileNotFoundError: [Errno 2] No such file or directory: 'datab.yaml'
I have tried
import os.path
prog = ("/Users/jeram/Documents/NCI/Semester3/TriagatronX50")
directory = os.path.dirname(os.path.abspath(prog))
print(os.path.join(directory, 'datab.yaml'))
But it still throws the same error when I run it.
Any help very much appreciated!!
the problem is:
the directory of python file when executing is not the directory you put datab.yaml.
so, you should designate the whole abstract path of datab.yaml like the following:
import os.path
prog = ("/Users/jeram/Documents/NCI/Semester3/TriagatronX50")
directory = os.path.dirname(os.path.abspath(prog))
print(os.path.join(directory, 'datab.yaml'))
db = yaml.safe_load(open(os.path.join(directory, 'datab.yaml')))
and then execute it.
This question already has answers here:
FileNotFoundError: [Errno 2] No such file or directory [duplicate]
(6 answers)
Closed 1 year ago.
I got this error when I tried to open the csv file where I made the changes
I wanted to know the reason for this error (FileNotFoundError :
[Errno 2] No such file or directory: 'bahal.csv')
import csv
from statistics import mean
with open('bahal.csv') as f:
reader = csv.reader(f)
for row in reader:
name = row[0]
average = statistics.mean(row[1:])
javab = name , average
print(javab)
good prectise is to use full file names because with relative it is hard to keep track of where what is
if you run:
user#~$ python file.py
it will see just files in ~(home dir)
if you run:
user#~/workfolder$ python file.py
it will see ~/workfolder files and so on
main thing is what the working dir is
The file bahal. csv should be in the same folder as the pyton file you are working on
OR
Try including full path.
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 2 years ago.
I'm having an issue with "FileNotFoundError: [Errno 2] No such file or directory: 'Pronunciation_Data.txt'
I keep resetting my working directory to the correct location but it does not seem to want to stay (using Spyder/Anaconda). I have also tried the following:
file = open(r'C:\path\to\Pronunciation_Data.txt\Users\stephaniecheetham\Desktop\Thesis')
import os
os.chdir(r'C:\Users\stephaniecheetham\Desktop\Thesis')
file = open('Pronunciation_Data.txt')
file = open("Pronunciation_Data.txt",'r')
file = open("<Users/stephaniecheetham/Desktop/Thesis>\Pronunciation_Data.txt",'r')
file = open("C:/stephaniecheetham/Desktop/Thesis/Pronunciation_Data.txt",'r')
No luck with any of them. Just the same error. I had a recent issue with a module as well and fixed it using the import os command.
Any suggestion?
Does your directory exist? Check if your file exists by doing this:
import os
os.chdir('C:\Users\stephaniecheetham\Desktop\Thesis')
try {
file = open('Prononunciation_Data.txt')
} except FileNotFoundError {
print("File not found")
}
Does your directory exist? Check if the directory exists by using os.listdir()
They are 2 mistakes in your code:
You have not imported the os module
They is a SyntaxError in line 2, where you're a missing 1 parenthese
To help with debugging, you can also do os.listdir() to see if you are actually in the correct directory.
Also, it's not clear what the full path of your file should be. C:/stephaniecheetham is different from C:/Users/stephaniecheetham
This question already has answers here:
Python FTP get the most recent file by date
(5 answers)
Python FTP server download Latest File with specific keywords in filename
(1 answer)
Closed 4 years ago.
Good morning,
I'm trying to get the latest file in a folder stored in a ftp. That's why I created this function :
def getDataFromCSV_FTP(fileName, index_row_to_start):
"""
Create a data frame based on the csv named fileName and located on the FTP, starting to read at the index_row_to_start
"""
try :
list_of_files = ftp.nlst(fileName) # all files with following the path with csv format
latest_file = max(list_of_files, key=os.path.getctime)
df_from_csv = pd.read_csv(latest_file, skiprows=index_row_to_start, dtype="str")
return df_from_csv
except :
print ("Error while reading the file", fileName)
The function is able to find all files on the FTP (I got the list with all files) but I'm getting an issue when it executes the max(). It's telling me that the file doesn't exist (looks like it's looking on the local path) -> FileNotFoundError: [Errno 2] No such file or directory
I'm new with python and I'm missing something here
Please can someone help me to figure out what's going wrong ?
Thank you very much
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 6 months ago.
I have a small issue with a python program that I wrote to extract some information from a special text file. The loop (code below) needs to execute my function extract_zcoords() over 500 files (1 file gives one list) so that I can build a dataset.
import os
def extract_zcoord(filename):
f = open(filename, 'r')
... # do something with f
### LOOP OVER DIRECTORY
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
for filename in os.listdir(location):
extract_zcoord(filename)
THE ERROR:
The IOException No such file or directory is the one that occurs, so for some reason python is not accessing the files. I have checked directory pathname (location) and file permissions, and they are correct (read+write). Any ideas why an IOError would be reported when the files do exist and pathname is correct?
Any ideas what might be wrong?
Probably, you should use os.path.join when you call
zdata.extend(extract_zcoord(filename))
like this:
zdata.extend(extract_zcoord(os.path.join(location, filename)))
You need to join the dirname and filename into one complete path:
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
for filename in os.listdir(location):
filename = os.path.join(location, filename)