Reading multiple files "except IOError as exc:" - python-3.x

I was looking through some example of how to read multiple files of data and come across these codes pop-up a lot:
try:
...
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
But I couldn't see anyone trying to explain it so I was hoping if you guys can help me to understand what it is?
Here is an example:
import glob
import errno
...
#Create a list of the path of all .txt files
files_list = glob.glob(data_path)
#Iterate through the files in files_list
for file_name in files_list:
try:
#something
except IOError as exc:
if exc.errno != errno.EISDIR:
raise

Here, we'll go through these lines of code one by one.
except IOError as exc:
The above line just catches an exception and gets the error message in the variable exc. What the exception means is along the lines of
when a system function returns a system-related error, including I/O failures such as “file not found” or “disk full” (not for illegal argument types or other incidental errors). Python 3 Docs
Note in Python 3, this is now OSError as IOError is merged into it.
if exc.errno != errno.EISDIR:
This line will check the type of error. This is given by the error's errno attribute. Specifically, errno.EISDIR means the error given came because the 'file' was actually a folder / directory. More info can be found here :D.
raise
Now the if statement has gone through and checked the type of error. It only lets it through to the raise over here if the error type does NOT mean that the path given was a directory. This means the exception could mean anything from 'Permission Denied' to 'Out of Memory'.
Hope that helped you out :D

Related

FileNotFoundError, even though the file exists

I've made a small program in Python that reads text files. However, I keep getting the FileNotFoundError, even though the file exists. The seemingly problematic code of my code looks like this:
fileEntered = False
while not fileEntered:
try:
fileName = input("Enter file name: ")
file = open(fileName, "r")
fileEntered = True
fileContents = file.readlines()
file.close()
except FileNotFoundError:
print("File not found. Please try again.")
When asked for the file, I enter randomtext.txt (which is located in the same folder as the program), but it keeps throwing the FileNotFoundError (it keeps printing the line in the except block).
Will be able to tell whether you are getting Win Error 2 or Win Error 3 when you are opening the file? If Win Error 2, then the problem may be related to your current working directory. That means, the scripting is looking into a different folder eventhough the file exists. If you are getting Win Error 3, then it is a different issue.
I've amended your code to get exact error code.
fileName = input("Enter file name: ")
file = open(fileName, "r")
fileContents = file.readlines()
print(fileContents)
file.close()
I am getting FileNotFoundError: [Errno 2] No such file or directory: '454544.txt' (when there is not file exists - Errno 2]. When I tried with full path or relative path, the script works fine. Please try and get the actual error so that you can progress. If the file exists in your current working directory, then the script will work properly. Please change the working directory to try.

Python: "FileNotFoundError" Despite being able to print such files

I'm working on a Python3 script where the code walks through directories and sub-directories to pull out all gzipped warc files.
I'd like to also add that the files are not in my home directory
file_path = os.path.join('/nappa7/pip73/Service')
walk_file(parallel_bulk, file_path)
Perhaps python is not looking where i think it's looking, nevertheless, here is my walk_file functions:
def walk_file(bulk, file_path):
warc = warcat.model.WARC()
try:
for (file_path,dirs,files) in os.walk(file_path):
for filenames in files:
if filenames.endswith('.warc.gz'):
warc.load(filenames)
except ValueError:
pass
When I replace the warc.load(filenames) with a print statement like so:
if filenames.endswith('.warc.gz'):
print(filenames)
The filenames are printed out onto the console as expected. Therefore, It leads me to believe that python was able to succesfully locate all warc.gz files. However, when i try the warc.load(filenames), i get:
FileNotFoundError: [Errno 2] No such file or directory: 'Sample.warc.gz'
I can certainly use some guidance.
Thank you.
So for anyone else who has a similar issue:
changing the code to this worked:
warc.load(os.path.join(file_path, filenames))
You need to use os.path.join(file_path, filenames) instead of just filenames.
Otherwise the operating system will look for the file in the current directory instead of file_path.
(And why is filenames plural when it refers to a single filename?)

An error when loading a 2mb dataset of floating points (python)

Does any one know why i got an error of "FileNotFoundError: [Errno 2] No such file or directory: 'bcs.xlsx'" when i'm loading this file of size 2mb it has around 60,000 rows and 4 columns.
i tried using csv instead of xlsx but i get the same error and i've checked hundreds times that the script and the file are at he same directory.
This is because Python does not find your file, errors are not lying.
But there's a misunderstanding in your question, you checked that the file is in the same directory as your script, but that's not the check you have to do. You have to check the file is in the current working directory of your python script.
To see your current working directory, use:
import os
print(os.getcwd())
And as we're at it you can list this directory:
print(os.listdir())
I don't know how you execute your script, but if you're using a terminal emulator, a typical way to give a file name to a program is by argument, not hardcoding its name, like by using argparse. And if you do this way, your shell completion may help you naming your file properly, like:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file.read())
Now on a shell if you type:
python3 ./thescript.py ./th[TAB]
your shell will autocomplete "./th" to "./thescript.py" (if and only if it exists), highly reducing the probablity of having a typo. Typically if there's a space in the filename like "the script.py", your shell should properly autocomplete the\ script.py.
Also if you use argparse with the argparse.FileType as I did, you'll have a verbose error in case the file does not exist:
thescript.py: error: argument file: can't open 'foo': [Errno 2] No such file or directory: 'foo'
But… you already have a verbose error.

How to catch error and stop using IF and ELSE statement?

Hello i am new to python programming. I am self learning by doing a project.
the code below is working fine, but i want to catch the exception if the condition is FALSE. i already have a statement to print if the directory is not there; and continue. I know i can make this code work by nesting the if statement.
but i dont want to do. i want to proceed to next line of code. when i run this as it is, i will get an error if the directory is missing.
import os
goToDir = open("URPsetup.dat","r")
goToDirPath = goToDir.read()
if (os.path.exists(goToDirPath))== True:
os.chdir(goToDirPath)
else: print("directory not found, please check file URPsetup.dat")
goToConfig = open("utility.dat", "r")
print(goToConfig.read())
i get this error message when the directory is not there. What i mean here is that, the directory i supplied in "URPsetup.dat" was incorrect. I did this on purpose to see if the code will stop at the else statement above. it will print the else statement, and continue on to show the error message below. how do i catch this error and stop?
directory not found, please check file URPsetup.dat
Traceback (most recent call last):
File "C:/Users/R82436/PycharmProjects/URP2017/prototype.py", line 9, in <module>
goToConfig = open("utility.dat", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'utility.dat'
directory not found, please check file URPsetup.dat
Process finished with exit code 1
I GUESS you're looking for this:
import sys
.
.
else:
print("directory not found, please check file URPsetup.dat")
sys.exit()
.
.
OR this will do just fine too:
# No need to import sys
.
.
else:
print("directory not found, please check file URPsetup.dat")
raise SystemExit(0)
.
.
That will do the trick ;)
And try to explain your problem properly next time, You aren't completely clear on what is it exactly that you want to do.

Python3. Prompt requires ctrl+c and halts script

I'm writing a small script that runs through a directory and attempts to obtain version numbers by running "filename --version". Now for the most part it works or fails in a manageable way. However I've come across one file "iptables-restore" in /sbin/ that fails when --version is passed to it. However the failure leaves the prompt in a state that requires a ctrl+z or ctrl+c to return to the prompt, and thus halts my script.
Here's the code:
try:
subOut = subprocess.check_output([fname, "--version"])
except subprocess.CalledProcessError as cpE:
fObj.write('{0:25}RETURN CODE:{1:15}\t\t{2:30}\n'.format(fnamecolon, cpE.returncode, cpE.output))
fnamecolon = ''
pass
except Exception as e:
pass
I just wondered if there's an elegant way to handle this - either via a return code or perhaps an Exception.
I'm fairly new to Python and am doing this for practice really - so any guidance is greatly appreciated.
Thanks all!
So this works better - probably still some things I'm not understanding though...
try:
# devnull=open(os.devnull, 'w')
try:
from subprocess import DEVNULL # Python 3
except ImportError:
DEVNULL = open(os.devnull, 'r+b', 0)
subOut = Popen([fname, "--version"], stdin=DEVNULL, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = subOut.stdout.read()
except subprocess.CalledProcessError as cpE:
fObj.write('{0:25}RETURN CODE:{1:15}\t\t{2:30}\n'.format(fnamecolon, cpE.returncode, cpE.output))
fname = ''
pass
except Exception as e:
# fObj.write('{0:30}{1:30}{2:30}\n'.format(fname, e.returncode, e.output))
pass
I can reproduce it: iptables-restore --version commands waits for input without stdin=subprocess.DEVNULL suggested by #mata:
#!/usr/bin/env python3
import subprocess
cp = subprocess.run(['iptables-restore', '--version'],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(cp.returncode, cp.stdout, cp.stderr)

Resources