I'm trying to access a folder and read the text files in it. Eventually I'm going to build them into a basic dictionary to dump into JSON but something's going on with the files. I'm getting an error message:
FileNotFoundError: [Errno 2] No such file or directory: ___
The thing is, this code IS working on my test files ... so maybe it's somehow a file issue?
import json
import os
for file in os.listdir("filepath"):
with open ('%s.json' % file, 'w') as fp:
file = f.read()
f = open(file, 'r')
print (f)
These are the text files not getting 'found/read':
https://drive.google.com/open?id=0B4zJC6biI6jERDFHSzZvUUJaRE0
Has anyone else run into this problem / found a solution that might work?
Related
I'm trying to load .nii-files with the nibabel library and am running into the following error with the .nii-files:
>>> path = '..\\..\\data\\raw\\data\\images\\AD\\female\\002_S_0938\\MPR__GradWarp__B1_Correction__N3__Scaled\\2006-10-05_15_54_26.0\\S19852\\ADNI_002_S_0938_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070219175406282_S19852_I40980.nii'
>>> with open(Path(path), 'rb') as f:
>>> print('great success!')
FileNotFoundError: [Errno 2] No such file or directory: '..\\..\\data\\raw\\data\\images\\AD\\female\\005_S_1341\\MPR__GradWarp__B1_Correction__N3__Scaled\\2007-03-07_12_44_49.0\\S27673\\ADNI_005_S_1341_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070717180348670_S27673_I60417.nii'
And with the test-file:
>>> path = '..\\..\\data\\raw\\data\\images\\AD\\female\\002_S_0938\\MPR__GradWarp__B1_Correction__N3__Scaled\\2006-10-05_15_54_26.0\\S19852\\test.txt'
>>> with open(Path(path), 'rb') as f:
>>> print('great success!')
great success!
Alternatively, the problem still exists with the library designed to read .nii-files:
>>> import nibabel as nib
>>> path = '..\\..\\data\\raw\\data\\images\\AD\\female\\002_S_0938\\MPR__GradWarp__B1_Correction__N3__Scaled\\2006-10-05_15_54_26.0\\S19852\\ADNI_002_S_0938_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070219175406282_S19852_I40980.nii'
>>> nib.load(path)
FileNotFoundError: No such file or no access: '..\..\data\raw\data\images\AD\female\005_S_1341\MPR__GradWarp__B1_Correction__N3__Scaled\2007-03-07_12_44_49.0\S27673\ADNI_005_S_1341_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070717180348670_S27673_I60417.nii'
And with the test file:
>>> path = '..\\..\\data\\raw\\data\\images\\AD\\female\\002_S_0938\\MPR__GradWarp__B1_Correction__N3__Scaled\\2006-10-05_15_54_26.0\\S19852\\test.txt'
>>> nib.load(path)
ImageFileError: Empty file: '..\..\data\raw\data\images\AD\female\002_S_0938\MPR__GradWarp__B1_Correction__N3__Scaled\2006-10-05_15_54_26.0\S19852\test.txt'
which in my opinion is strange behavior as the library finds the text file but not the .nii file, which exists in the same directory.
Note:
the .nii files are not corruputed as I can open them in a separate program with no problem.
I operate a windows OS
Update:
Current file structure:
Full traceback:
Since os.listdir('..\\..\\data\\raw\\data\\images\\AD\\female\\002_S_0938\\MPR__GradWarp__B1_Correction__N3__Scaled\\2006-10-05_15_54_26.0\\S19852\\') returns both files, and you can open the file test.txt but not 'ADNI_002_S_0938_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070219175406282_S19852_I40980.nii', it suggests the issue might be related to the long filepath.
The 260 character limit can be disabled by setting this registry key to 1: Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled
See Enable Long Paths in Windows 10, Version 1607, and Later
I'm simply trying to alter a CSV file with Python.
When all the files were in the same dir, everything was fine.
Now that the input files are in a different dir than where the output files will be, everything blows up, apparently b/c the files do not exist?
I first found this:
open() in Python does not create a file if it doesn't exist
Then I learned to change to the directory, which helped me loop over the CSVs in the target dir:
Moving up one directory in Python
When I run the command:
python KWRG.py ../Weekly\ Reports\ -\ Inbound/Search\ Activity/ 8/9/2021
I will get:
Traceback (most recent call last): File "KWRG.py", line 15, in <module> with open(args.input, 'r') as in_file, open(args.output, 'w') as out_file: IsADirectoryError: [Errno 21] Is a directory: '../Weekly Reports - Inbound/Search Activity/'
Sorry If I'm missing the obvious here, but why is the file not being created in the directory that I'm pointing the script to (or at all for that matter)?
The code:
import csv
import argparse
import os
# Create a parser to take arguments
#...snip...
cur_dir = os.getcwd()
reports_dir = os.chdir(os.path.join(cur_dir, args.dir))
for csv_file in os.listdir(reports_dir):
# Shorthand the name of the file
#...snip...
# Open the in and out files
with open(csv_file, 'r') as in_file, open(f'{out_name}-Search-Activity-{args.date}.csv', 'w+') as out_file:
# Re-arrange CSV
# EOF
Your problem is with this line:
reports_dir = os.chdir(os.path.join(cur_dir, args.dir))
os.chdir() doesn't return anything, it just performs the operation requested - changing the current working directory. From an interactive session with the REPL:
>>> import os
>>> result = os.chdir("/Users/mattdmo/")
>>> result
>>>
For your purposes, all you need is
reports_dir = os.path.join(cur_dir, args.dir)
and you'll be all set.
I tried using open but it gives an error that the folder doesn't exist, which makes no sense since this is a command to create a folder, not read one. I saw Automatically creating directories with file output, but there is an error saying this is a Errno 30: Read only system: "/folder". Does anyone know how to avoid Error 30?
My code so far:
import os
filename = "/folder/y.txt"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write("FOOBAR")
I figured i just shouldn't put the slash behind the "folder"filename = "folder/y.txt" os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "w") as f: f.write("FOOBAR")
I want to import all data using path but I keep getting error. And how can I get an output in a separate file?
Input File:
import os,json
path= "./Responses"
for file in os.listdir(path):
with open (file) as json_file:
data=json.load(json_file)
Error Message:
FileNotFoundError: [Errno 2] No such file or directory: 'data_1.json'
And can I separate the new data by using 'Out'?
Output File:
for file in os.listdir(path):
with open(json_data, 'w') as outfile:
json.dump('Out'+ data, outfile, indent=4)
The reason why you got error because python script cannot find the relative path for the file founded in "with open (file) as json_file:"
To fix that you can change that to:
with open (path +'/'+ file) as json_file:
I've been trying to download files from an FTP server. For this I've found this Python-FTP download all files in directory and examined it. Anyways, I extracted the code I needed and it shows as follows:
import os
from ftplib import FTP
ftp = FTP("ftp.example.com", "exampleUsername", "examplePWD")
file_names = ftp.nlst("\public_html")
print file_names
for filename in file_names:
if os.path.splitext(filename)[1] != "":
local_filename = os.path.join(os.getcwd(), "Download", filename)
local_file = open(filename, 'wb')
ftp.retrbinary('RETR ' + filename, local_file.write)
local_file.close()
ftp.close()
But when it tries to open the file, it keeps saying:
ftplib.error_perm: 550 Can't open CHANGELOG.php: No such file or directory
I've tried w+, a+, rw, etc. and I keep getting the same error all the time. Any ideas?
Note: I am using OSX Mavericks and Python 2.7.5.
This question may have been asked several times and believe me I researched and found some of them and none of them worked for me.
open() in Python does not create a file if it doesn't exist
ftplib file select
It looks like you are listing files in a directory and then getting files based on the returned strings. Does nlst() return full paths or just filenames? If its just filenames than retrbinary might be expecting "/Foo/file" but getting "file", and there might not be anything named file in the root dir of the server.