This question already has answers here:
How can I open multiple files using "with open" in Python?
(8 answers)
Closed 4 years ago.
Hi i was going to open multiple txt files in a directory.
But i get error message of
File "testTopic.py", line 9, in <module>
with open(path +i, 'r') as f:
IOError: [Errno 2] No such file or directory: 'C:\Users\Documents\FP\TM\Export28011986676_10155756928931677.txt'
I did import os, and i trying to open files that ends with '.txt'
where by my files all are 122343.txt, 344545.txt, 565464353.txt and carry on.
You need to add a separator between the path and the file name:
path = "C:\Users\Documents\FP\TM\Export\\"
(Mind the slash at the end of the path.)
or:
you can useing os.path.join
import os
os.path.join(path,i)
Related
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:
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:
How to open every file in a folder
(8 answers)
Closed 2 years ago.
How can I read multiple txt file from a single folder in Python?
I tried with the following code but it is not working.
import glob
import errno
path = '/home/student/Desktop/thesis/ndtvnews/garbage'
files = glob.glob(path)
for name in files:
try:
with open(name) as f:
print name
for line in f:
print line,
f.close()
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
Your glob isn't correct. You should add a /* to the end of your path to select all files (or directories) in your path, and then check if they are files with os.path.isfile. Something like:
from os.path import isfile
files=filter(isfile,glob.glob('%s/*'%path))
You also have an issue with the actual opening. When your with statement ends, the file is closed and f is no longer accessible. Anything you do with the file should be under the with statement. And you shouldn't explicitly close it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
IOError when trying to open existing files
I'm having problems opening a file with open() in python 3.3, any idea why?
I'm trying
import os
filelist = [ f for f in os.listdir( os.curdir )]
singleFile = filelist[a]
hppfile = open(singleFile, 'r')
And I get
FileNotFoundError: [Errno 2] No such file or directory: '-file that is actually inside the directory-'
Ideas?
On Windows, I just started this to learn this to write few quick scripts
If you read the documentation for listdir you will see that it returns filenames and not full path.
You will need something like
current_dir_path = os.getcwd()
open(os.path.join(curren_dir_path, file), 'r')
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)