shutil.copyfile() throws Errno 2 exception on destination file path - python

To setup this question, I'm using Python in the form of Jython 2.7 (in Ignition SCADA). I have a function that copies image files to a print spool network folder. Occasionally I get an error like the following:
An ERROR occured: Line - 241: <type 'exceptions.IOError'>, [Errno 2] No such file or directory: u'\\server23\eFormz\D1234567.tif'. Part - ABC1234X12, while printing label.
These print jobs run through a number of different parts in the same order but only 1 or 2 generate this error. All others copy the file as expected. The error is calling out the DESTINATION file path in all cases. The destination folder exists as all of the good copies are going to the same folder as those that cause errors.
The code that does the copy looks like this:
import shutil
imgFiles = glob.glob(labelImage)
if len(imgFiles) > 0:
imgFile = imgFiles[0]
#ensure the image file path is actually a file
if os.path.isfile(imgFile):
fileName = imgFile.rsplit('\\', 1)[1]
dstFP = '\\\\server23\\eFormz\\' + fileName
shutil.copyfile(imgFile, dstFP)
Since this works most of the time, with the same folder paths and only the file name changing (all the source files are confirmed to exist and confirmed that they are indeed files, before attempting the copy), I'm perplexed as to the cause. File and folder permissions are the same for each so that should not be the cause.
Any ideas as to what may be causing this or a way to more effectively troubleshoot it would be appreciated.
Thanks.

Related

FileNotFoundError, when I run shutil.copy, 1 particular ".dylib" file throws FileNotFoundError error every time

I am making a python app and one of it's functions involves copying the contents of one directory to several different locations. Some of these files are copied to the same directory in which they currently reside and are renamed. It almost always works except when it hit this one particular file.
This is the code that does the copying.
shutil.copy(original_path_and_file,os.path.join(redun.path, redun.file_name))
The particular copy that causes the crash is when it's trying to copy /Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.dylib to /Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.0.dylib
This is the error that appears in my terminal:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.dylib'
Why does this file throw the error and none of the other files that it copies throw any errors?
Update
The file is actually an "Alias" according to finder:
Perhaps you could attempt to solve it by connecting the directory path and the filename like follows:
One problem here is that you do not specify the path of the file. As you are executing the command from the parent directory, the script has no way of knowing that testfile2.txt is in a subdirectory of your input directory. To fix this, use:
shutil.copy(os.path.join(foldername, filename), copyDirAbs)
or
# Recursively walk the Search Directory, copying matching files
# to the Copy Directory
for foldername, subfolders, filenames in os.walk(searchDirAbs):
print('Searching files in %s...' % (foldername))
for filename in filenames:
if filename.endswith('.%s' % extension):
print('Copying ' + filename)
print('Copying to ' + copyDirAbs)
totalCopyPath = os.path.join(searchDirAbs, filename)
shutil.copy(totalCopyPath, copyDirAbs)
print('Done.')
The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.
The way I fixed this was by having the app ignore link files. I wrote this:
if not os.path.islink(original_path_and_file):
shutil.copy(original_path_and_file,os.path.join(redun.path, redun.file_name))
This is technically more of a work-around than a fix because it doesn't actually copy the file but I decided the app still works fine when not copying link files.

Not finding any solution to [Errno 2]

I'm trying to use a program to read from a file with pi-digits. The program and the text file with the pi-digits are in the same directory, but i still get the error message :
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents.rstrip())
Traceback (most recent call last):
File "C:\Python\Python_Work\python_crash_course\files_and_exceptions\file_reader.py", line 1, in <module>
with open('pi_digits.txt') as file_object:
FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'
I have looked for a solution but haven't found any.
I found a piece of code which supposedly shows me what the working directory is. I get an output that shows a directory that is 2 steps above the directory i have my programs and text file inside.
import os
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
So when i put the pi text document in the directory that the output is showing (>python_work), the program is working. When it does not work is when the text file is in ">files_and_exceptions" which is the same file the program itself is inside. My directory looks like this when it is not working:
>python_work
>python_crash_course
>files_and_exceptions
file_reader.py
pi_digits.txt
show_working_directory.py
And like this when it is working:
>python_work
pi_digits.txt
>python_crash_course
>files_and_exceptions
file_reader.py
show_working_directory.py
I'm new to python and really appreciate any help.
Thanks!
Relative path (one not starting with a leading /) is relative to some directory. In this case (and generally*), it's relative to the current working directory of the process.
In your case, given the information you've provided, for it would be "python_crash_course/files_and_exceptions/pi_digits.txt" in the first case as you appear to be running the script from python_work directory.
If you know the file to be in the same directory as the script itself, you could also say:
import os.path
...
os.path.join(os.path.dirname(__file__), "pi_digits.txt")
instead of "pi_digits.txt". Or the same using pathlib:
from pathlib import Path
...
Path(__file__).with_name("pi_digits.txt")
Actually unless you have a point to anchor to like the script itself, using relative filename / path (using absolute paths brings its own problems too) in the code directly is rather fragile and in that case getting it as a parameter of a function (and ultimately argument of CLI or script call in general) or alternatively reading it from standard input would be more robust.
* I would not make that an absolute statement, because there are situations and functions that can explicitly provide different anchor point (e.g. os.path.relpath or openat(2); or as another example a symlink target)

FileNotFound Error: [Errno 2] No such file or directory b when iterating through list of files in Python on Windows?

I am getting a FileNotFound error when iterating through a list of files in Python on Windows.
The specific error I get looks like:
FileNotFoundError: File b'fileName.csv' does not exist
In my code, I first ask for input on where the file is located and generate a list using os (though I also tried glob):
directory = input('In what directory are your files located?')
fileList = [s for s in os.listdir(directory) if s.endswith('.csv')]
When I print the list, it does not contain byte b before any strings, as expected (but I still checked). My code seems to break at this step, which generates the error:
for file in fileList:
pd.read_csv(file) # breaks at this line
I have tried everything I could find on Stack Overflow to solve this problem. That includes:
Putting an r or b or rb right before the path string
Using both the relative and absolute file paths
Trying different variations of path separators (/, \, \\, etc.)
I've been dealing with Windows-related issues lately (since I normally work in Mac or Linux) so that was my first suspicion. I'd love another set of eyes to help me figure out where the snag is.
A2A.
Although the list was generated correctly because the full directory path was used, the working directory when the file was being run was .. You can verify this by running os.getcwd(). When later iterating through the list of file names, the program could not find those file names in the . directory, as it shouldn't. That list is just a list of file names; there was no directory tied to it so it used the current directory.
The easiest fix here is to change the directory the file is running through after the input. So,
directory = input('In what directory are your files located?')
os.chdir(directory) # Points os to given directory to avoid byte issue
If you need to access multiple directories, you could either do this right before you switch to each directory or save the full file path into your list instead.

Python - [Errno 2] No such file or directory,

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.

Errno 2 using python shutil.py No such file or directory for file destination

I am using the shutil python module to copy files and directories on a linux redhat machine.
I wrote the following method, which takes in 2 params: src (the path of the file or dir that is being collected) and destination (the desired new path of where the collected log/dir is being pasted to).
def copy(src, destination):
if(os.path.exists(src)):
if(os.path.isdir(src)):
if(os.path.exists(destination)):
shutil.copytree(src, destination+getTimeStamp())
else:
shutil.copytree(src, destination)
else:
shutil.copy(src, destination)
else:
print src+" not found"
I have been using this method just fine, but I recently ran into an error when running this code:
copy("/home/midgar/logs/logger.xml", currentPath+"/testrun/logs/logger.xml")
The error: IOError: [Errno 2] No such file or directory: 'collectedLogs/testrun/logs/logger.xml'
I would understand what this error implies if the file or directory that it is looking for is the src, but this is the destination that is causing the error. I found out that this line of code that throws the error goes to the line: "shutil.copy(src, destination)" in my copy method.
So far, my copy method just overwrites existing files, and if there is an existing directory, it creates a new one with a time stamp. In this case, the destination file does not already exist anyways. So, what can be the issue? Why am I getting this error with the DESTINATION path (when I would typically expect to see this kind of error with the SRC path).
Could it possibly be because this is an .xml file?
When I get this error it usually means that one of the folders doesn't exist.
I wrote a simple script to to test this out. In the script below the backup folder does exist but the today folder does not. When I run the script I get the same error as you.
IOError: [Errno 2] No such file or directory: 'backup/today/my_file.txt'
import shutil
shutil.copy("my_file.txt", "backup/today/my_file.txt")
If all of your folders do exist I would check to make sure the permissions on them have not changed.
By default, shutil.copytree() follows (resolves) symbolic links. If the symlink is broken, it raises a No such file or directory exception. One workaround is to specify that symlinks should be copied unresolved by passing symlinks=True.
I also recently came across this error. In my case, the file wouldn't be created because the filename and directory structure exceeded the max 260 characters. Solution: choose a shorter filename (or full file path name).

Categories