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')])
Related
This question already has answers here:
Open file in a relative location in Python
(14 answers)
Closed 7 months ago.
I'm new to coding so I followed a tutorial for a chatbot, I need to import my intentions (which I named intentii, in my language) intentii.json into the chatbot, so I'm using:
intentii = json.loads(open('intentii.json').read())
I have seen other questions about the error I said in the title, and yes, I made sure the name is typed right, it's in the same exact directory as all my files including this chatbot one are, and still, it says it can't find the file, I tried placing the whole directory path and it seemed to work ( I didn't get the same error again, but an unicode error which I believe is another problem related to my .json file ), but I cannot use the whole path because I have to send this to my teacher and, of course, he won't have the same exact path as mine.
What is the solution?
Edit: Also, I've noticed that it says that for every file I need to access from the folder
Try this function to load a json file:
import json
def load_json(filepath):
with open(filepath) as f:
data = json.load(f)
return data
Then
intentii = load_json("intentii.json")
Try using this python os module to find dir_root where current file is located and then combine it json file name if it is stored in same directory.
import json
import os
dir_root = os.path.dirname(os.path.abspath(__file__))
intentii1 = json.loads(open(dir_root+'\js.json').read())
This question already has answers here:
Why does the 260 character path length limit exist in Windows?
(11 answers)
Closed 2 years ago.
I am trying to iteratively open some files to do some processing with the data. However, I haven't been able to make it work. I don't know what could be causing this.
sd = os.path.dirname(os.path.abspath(__file__))
file_names = []
for root,d_names,f_names in os.walk(os.path.join(sd, path)):
for f in f_names:
if f.endswith('.csv'):
file_names.append(os.path.join(root, f))
for f_name in file_names:
with open(f_name, 'r') as file:
...
I have also tried the following aproach, using pathlib
input_path = pathlib.Path(path)
file_names = input_path.glob('**/*.csv')
for f_name in file_names:
with open(f_name.resolve(), 'r') as file:
...
Both methods yield the same result.
'path' is the name of a directory that sits on the same directory as the script. Reading the error seems to indicate the path is correct. The files sit in a somewhat complex file structure with pretty long filenames at times.
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\...
To give a bit more insight, here is a brief simplified representation of the file structure of path
path
¦-dir1
¦¦-dir2
¦¦¦-dir3
¦¦¦¦-sub1
¦¦¦¦¦-file-1a
¦¦¦¦-sub2
¦¦¦¦¦-file-1b
¦¦¦¦¦-file-2b
What I've found by testing is that when I replace path by dir3 to remove uneccessary traversal, the script will process file-1a which is the only one in that directory and file-1b, but give the same error when reaching file-2b. Furthermore, when making sub2 the target instead, it will process all files inside sub2 with no issues.
Also, as suggested, I tried adding the line print(os.access(f_name, os.R_OK), repr(f_name)) just before attempting to open the file. It turns out it returns False every time just before the error is raised(followed by the file path), and returns True whenever I've managed to process a file.
Many thanks to #ekhumoro for pointing me in the right direction.
It seems my paths were longer than 260 characters, which is by default not allowed by Windows for backwards-compatibility reasons.
I changed the Windows registry to allow long paths and now my script has no issues accessing all the files in the structure.
Simple enough situation; I'm working from within a directory which contains a script, and a subdirectory at the same level which contains many JSON files.
Using ideally Python, I'd like to combine all of the JSON files into one. Depending on your suggestion, this may leave behind redundant headers, but I can pop those off the JSON as I convert that file into a python dictionary object. Not a problem.
The problem is that I have been unable to combine the files into one. I'm practicing on text files for a start, to no avail. I'm using the python "os" module, but no luck. Keenly;
path = "/Users/me/ScriptsAndData/BagOfJSON"
...
for filename in os.listdir(path):
with open(filename, 'rb') as read file:
....
Results in the error;
with open(filename, 'rb') as readfile:
FileNotFoundError: [Errno 2] No such file or directory: 'firstFile.JSON'
And this finds and names the first file from within the directory, but doesn't operate on it like a file.
tldr;
I'm trying to merge multiple JSON files, all located within a single directory, into a single JSON file. If you know how to do this for any filetype, I'd be happy to know how you do it, then build from there.
Cheers!
I am trying to make a minor modification to a python script made by my predecessor and I have bumped into a problem. I have studied programming, but coding is not my profession.
The python script processes SQL queries and writes them to an excel file, there is a folder where all the queries are kept in .txt format. The script creates a list of the queries found in the folder and goes through them one by one in a for cycle.
My problem is if I want to rename or add a query in the folder, I get a "[Errno 2] No such file or directory" error. The script uses relative path so I am puzzled why does it keep making errors for non-existing files.
queries_pathNIC = "./queriesNIC/"
def queriesDirer():
global filelist
l = 0
filelist = []
for file in os.listdir(queries_pathNIC):
if file.endswith(".txt"):
l+=1
filelist.append(file)
return(l)
Where the problem arises in the main function:
for round in range(0,queriesDirer()):
print ("\nQuery :",filelist[round])
file_query = open(queries_pathNIC+filelist[round],'r'); # problem on this line
file_query = str(file_query.read())
Contents of queriesNIC folder
00_1_Hardware_WelcomeNew.txt
00_2_Software_WelcomeNew.txt
00_3_Software_WelcomeNew_AUTORENEW.txt
The scripts runs without a problem, but if I change the first query name to
"00_1_Hardware_WelcomeNew_sth.txt" or anything different, I get the following error message:
FileNotFoundError: [Errno 2] No such file or directory: './queriesNIC/00_1_Hardware_WelcomeNew.txt'
I have also tried adding new text files to the folder (example: "00_1_Hardware_Other.txt") and the script simply skips processing the ones I added altogether and only goes with the original files.
I am using Python 3.4.
Does anyone have any suggestions what might be the problem?
Thank you
The following approach would be an improvement. The glob module can produce a list of files ending with .txt quite easily without needing to create a list.
import glob, os
queries_pathNIC = "./queriesNIC/"
def queriesDirer(directory):
return glob.glob(os.path.join(directory, "*.txt"))
for file_name in queriesDirer(queries_pathNIC):
print ("Query :", file_name)
with open(file_name, 'r') as f_query:
file_query = f_query.read()
From the sample you have given, it is not clear if you need further access to the round variable or the file list.
import os, csv
f=open("C:\\tempa\\file.csv", 'wb') #write to an existing blank csv file
w=csv.writer(f)
for path, dirs, files, in os.walk("C:\\tempa"):
for filename in files:
w.writerow([filename])
running win7 64bit latest python, using anaconda spyder, pyscripter issue persists regardless of the ide.
I have some media in folders in tempa jpg, pdf and mov... and I wanted to get a file list of all of them, and the code works but it stops without any issue at row 113, nothing special with the file it stops on, no weird characters.
I could have 3 blocks of code one for each folder to work around this weird bug. but it shouldnt have an issue.. the folders are all in the root folder without going too deep in sub folders:
C:\
-tempa
-jpg
-pdf
-mov
I have heard there are issues with os.walk but I didn't expext anything weird like this.
Maybe I need an f=close?
You were examining the file before it was fully closed. (f won't be closed until, at least, it is no longer referenced by any in-scope variable name.) If you examine a file before it is closed, you may not see the final, partial, data buffer.
Use the file object's context manager to ensure that the file is flushed and closed in all cases:
import os, csv
with open("C:\\tempa\\file.csv", 'wb') as f: #write to an existing blank csv file
w=csv.writer(f)
for path, dirs, files, in os.walk("C:\\tempa"):
for filename in files:
w.writerow([filename])
# Now no need for f.close()