os.path.getctime for a ftp file [duplicate] - python

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

Related

Can't figure out where this error is coming from [duplicate]

This question already has an answer here:
Can't Open files from a directory in python [duplicate]
(1 answer)
Closed 1 year ago.
I'm working on a sentiment analysis program for an NLP class. I've imported a folder with a lot of different files and my goal is to merge all of the files text together to analyze it as one big pool of text. So far I've been able to import all of the files using:
path = "noblespeeches"
nobel_speeches = os.listdir(path)
files = sorted([file for file in nobel_speeches if file.endswith('.txt')])
...where "nobelspeeches" is the path to the appropriate file on my computer. I've tested this part and it seems to work fine. I'm having trouble creating a function to read the files so I can merge all of the text together.
def read_file(file_name):
with open(file_name, 'r+', encoding='utf-8') as file:
file_text = file.read()
return file_text
This function is what I've been working with, and I cannot seem to get it to work for the life of me. I'm sure the answer is quite simple, but I'm fairly new to Python and very new to NLP. I've been researching different possible solutions to no avail.
The error code is: FileNotFoundError: [Errno 2] No such file or directory:
The machine is producing an error that states that the first file in my folder doesn't exist even though it will acknowledge the file in the earlier code.
Chang this
files = sorted([os.path.join(path, file) for file in nobel_speeches if file.endswith('.txt')])

Exception occurring when trying to open a file with python [duplicate]

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

No such file or directory? [duplicate]

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

Error opening file - Python 3.3 [duplicate]

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')

IOError when trying to open existing files [duplicate]

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)

Categories