I want to iterate through files in a directory and test each of them to see if they meet my selection criteria. Python is finding my file, and then immediately claiming to have not found my file. I am not sure what is going on.
import pandas as pd
for subdir, dirs, files in os.walk(path):
for file in files:
print('file %s located' %file)
in_file=pd.read_csv(file)
The error goes as follows:
runfile('F:/School/Research/WGM/NewProject/PythonScripts/EqiSiteSorter.py', wdir='F:/School/Research/WGM/NewProject/PythonScripts')
file FileName.csv located
Traceback (most recent call last):
FileNotFoundError: [Errno 2] File b'FileName.csv' does not exist: b'FileName.csv'
It successfully prints the message which states that the file was located, the file name is stored as a string in my variables, but apparently it suddenly got lost after that. Help please...
I thinks because of the directories. Inside the read calc try suing os.path.join(dir,filename)
This should work.
Related
I'm a newbie for writing here. So please please be bear with me.
I'm running this code to open my file and I put this in right directory 'data'. But my python sent me error message continuely.
I wrote this,
#file = unidecode.unidecode(open('./data/input.txt').read())
#file = unidecode.unidecode(open('./data/linux.txt').read())
file = unidecode.unidecode(open('./data/hh1.txt').read())
file_len = len(file)
print('file_len =', file_len)
and poped up this
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-36-aa7f0f650918> in <module>()
4 #file = unidecode.unidecode(open('./data/linux.txt').read())
5
----> 6 file = unidecode.unidecode(open('./data/hh1.txt').read())
7 file_len = len(file)
8 print('file_len =', file_len)
FileNotFoundError: [Errno 2] No such file or directory: './data/hh1.txt
code image
directory
It's part of RNN(Recurrent Neural Network) code and it's part of processing text data to learn knitting pattern.
It's very simple error but I can't find a good way out.... So thank you for your patient to read this and I hope someone could help me out
You're trying to open a file that is inside data folder in your relative path.
open('./data/hh1.txt').read()
If your script is in
/home/user/test.py
This one tries to open:
/home/user/data/hh1.txt
And if you use
open('./hh1.txt').read()
This one tries to open
/home/user/hh1.txt
That is in the same directory of your script.
You can use:
import os
print(os.listdir())
And it will show you all files in the current directory.
If you're using a relative path, check the path from your current directory to the destination file.
It's likely that the path that you have entered is being interpreted differently than expected. This depends on a variety of this such as where the Python file you are executing is and whether it is part of a bigger project.
A good way to debug this is to expand the path you are trying to use into its absolute path. You can do this using the following code:
import os
print(os.path.abspath("./data/hh1.txt"))
This will output something like "/home/user/project/data/hh1.txt".
You can check the output of this and verify that your files are in the right location or if your path is possibly incorrect.
I am trying to write a program to categorize into folders a large amount of files according to their respective groups indicated in the file name. I wrote the followin code, but when I run it it gives me a file not found error, even though the file is in the given path. I'd appreciate any help in figuring out what is wrong.
import os
old_dir = '/Users/User/Desktop/MyFolder'
for f in os.listdir(old_dir):
file_name, file_ext = os.path.splitext(f)
file_name.split('-')
split_file_name = file_name.split('-')
new_dir = os.path.join(old_dir,
'-'.join(split_file_name[:3]),
split_file_name[5],
f)
os.rename(os.path.join(old_dir, f), new_dir)
Here's the error:
Traceback (most recent call last):
File "/Users/User/Documents/Sort Files into Folders/Sort Files into Folders.py", line 19, in <module>
os.rename(os.path.join(old_dir, f), new_dir)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/User/Desktop/MyFolder/AHA35-3_30x1_12-31-7d-g1a1-ArmPro.jpg' -> '/Users/User/Desktop/MyFolder/AHA35-3_30x1_12-31/ArmPro/AHA35-3_30x1_12-31-7d-g1a1-ArmPro.jpg
os.rename does not automatically create new directories (recursively), if the new name happens to be a filename in a directory that does not exist.
To create the directories first, you can (in Python 3) use:
os.makedirs(dirname, exist_ok=True)
where dirname can contain subdirectories (existing or not).
Alternatively, use os.renames, that can handle new and intermediate directories. From the documentation:
Recursive directory or file renaming function. Works like rename(), except creation of any intermediate directories needed to make the new pathname good is attempted first
os.rename need path, so it should look like:
os.rename(path+old_name, path+new_name)
There is folder path:
P:\\2018\\Archive\\
There are many zipfiles I want to create programmatically, but am starting with test. I will name this test zip file "CO_007_II.zip" and will attempt to create in above location:
import zipfile as zp
with zp.ZipFile("P:\\2018\\Archive\\CO_007_II.zip",'w') as myzip:
myzip.write(r"P:\2018\CO_007_II")
But I get error!
...
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\ArcGIS10.2\lib\zipfile.py", line 752, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'P:\\2018\\Archive\\CO_007_II.zip'
Is this not method for creating new zipfile? I know file does not exist. Is why I am using 'w' mode, no?
This is documentation:
https://docs.python.org/3/library/zipfile.html
It says:
'w' to truncate and write a new file
Example on documentation page:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
code worked two days ago to create new zip file but did not add folder. Today nothing works! Why not? All paths valid. How do I create new zip file with python and add folders to it?
I also encountered a similar issue and came here looking for answers. Since this was the top hit, I'll add what I discovered.
The answer provided by #metatoaster didn't work for me, when stepping through the code I found that the path returned true to isdir.
In my case, the path length exceeded the Windows max path length (260 chars) which was causing it to fail despite the folder path being valid.
Hope that helps someone else down the line!
The only way this could be reproduced was to create a zipfile in a directory that does NOT exist yet. The only way to be sure (you cannot trust a file manager; only way to verify is to check from within the program itself) is to assign the desired path of the new zip file to a variable (e.g. path), and then call isdir(dirname(path)). For example:
from os.path import isdir
from os.path import dirname
target = "P:\\2018\\Archive\\CO_007_II.zip"
if not isdir(dirname(target)):
print('cannot create zipfile because target does not exists')
else:
# create the zipfile
I had the same issue. It was the long path. I solved by adding this //?/C at the beginning of the path
path = r"//?/C:\Users\Camilo\Proyectos"
i was just wondering how i can change the location/directory that python uses to navigate and open files.
I am a super noob so please use small words if you help me, and if you do, thanks.
In case it matter, i use two mass storage devices one is located under the A:\ and the other using the default C:. From memory i installed python under the A drive even though i know some parts are under the C drive. I also believe that i have set my mass storage devices up in AHCI or IDE.
Example Code:
File_Test = open("Test.txt", "r")
This then produces the error:
Traceback (most recent call last):
File "", line 1, in
File_Test = open("Test.txt", "r")
IOError: [Errno 2] No such file or directory: 'Test.txt'"
Which from what i understand is python can't find the directory under which thise file is located.
I would really like to know how to make python locate files in my specified directory. If you can help i would be very appreciative, thanks.
Use the os.chdir() function.
>>> import os
>>> os.getcwd()
'/home/username'
>>> os.chdir(r'/home/username/Downloads')
>>> os.getcwd()
'/home/username/Downloads'
You can get the current working directory using the os.getcwd function. The os.chdir function changes the current working directory to some other directory that you specify. (one which contains your file) and then you can open the file using a normal open(fileName, 'r') call.
More precisely, the problem is that there is no file "Test.txt" in the directory Python considers its current working directory. You can see which directory that is by calling os.getcwd. There are two solutions.
First, you can change Python's working directory by calling os.chdir to be the directory where your file lives. (This is what Sukrit's answer alludes to.)
import os
# Assuming file is at C:\some\dir\Test.txt
os.chdir("C:\some\dir")
file_test = open("Test.txt", "r")
Second, you can simply pass the full, absolute path name to open:
file_test = open("C:\some\dir\Test.txt")
I am trying to copy my Automater workflows, I have listed them in a config file, and I would like to loop through the config file and copy the directories. They have spaces in the names and I am having trouble.
It prints the filename correctly etc but the copy fails as there seems to be extra " " around the name with the copy
import os
import shutil
confdir=os.getenv("my_config")
dropbox=os.getenv("dropbox")
conffile = ('services.conf')
conffilename=os.path.join(confdir, conffile)
sourcedir= (r'~/Library/Services/')
destdir=os.path.join(dropbox, "My_backups")
for file_name in open(conffilename):
sourcefile=os.path.join(sourcedir, repr(file_name.strip()))
print sourcefile
destfile=os.path.join(destdir, file_name.strip())
shutil.copytree(sourcefile, destfile)
And the error is
~/Library/Services/'Add PDF Metadata.workflow'
Traceback (most recent call last):
File "Untitled 3.py", line 15, in <module>
shutil.copytree(sourcefile, destfile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 168, in copytree
names = os.listdir(src)
OSError: [Errno 2] No such file or directory: "~/Library/Services/'Add PDF Metadata.workflow'"
Thanks in advance
I have tried the suggestions below but it still is not working
Why are you using repr() on file_name.strip()? That will surround your filename with single quotes - and those aren't present in the file path. Remove the repr(), and it should work.
shutil.copytree(src, dst) will recursively copy a directory tree (and all the files in it) located at src to a new directory tree at dst. It is not meant to be used with files.
Here, you want to copy single files around, not a complete directory tree, you should just use shutil.copy or shutil.copy2.
If the files may be located in a directory tree that you want to reproduce, then you could use os.makedirs for the path returned by os.path.dirname(destfile) before calling shutil.copy(sourcefile) to actually copy the file to destfile.
However, be aware that calling os.makedirs with a destination that already exists will raise an error so you probably want to try / except.
It didn't like the ~ I put the full path in. I have also just edited one more bit and used sourcedir=os.path.expanduser('~/Library/Services/') to expand the home directory