Gunzip all the files present in source directory in Python - python

I have written a code to gunzip all the files present in the source folder. But I want to include the check that if gunzipped file doesn't exist then gunzip it else move to next file.
source_dir = "/Users/path"
dest_dir = "/Users/path/Documents/path"
for src_name in glob.glob(os.path.join(source_dir, '*.gz')):
base = os.path.basename(src_name)
dest_name = os.path.join(dest_dir, base[:-3])
with: gzip.open(src_name, 'rb') as infile, open(dest_name, 'wb') as outfile:
try:
for line in infile:
print ("outfile: %s" %outfile)
if not os.path.exists(dest_name):
outfile.write(line)
print( "converted: %s" %dest_name)
except EOFError:
print("End of file error occurred.")
except Exception:
print("Some error occurred.")
I have used os.path.exist to check whether the file exists or not, but it seems like os.path.exist doesn't work here.

I think you have misplaced the path.exists call. It should be:
source_dir = "/Users/path"
dest_dir = "/Users/path/Documents/path"
for src_name in glob.glob(os.path.join(source_dir, '*.gz')):
base = os.path.basename(src_name)
dest_name = os.path.join(dest_dir, base[:-3])
if not os.path.exists(dest_name):
with gzip.open(src_name, 'rb') as infile, open(dest_name, 'wb') as outfile:
try:
for line in infile:
print("outfile: %s" % outfile)
outfile.write(line)
print("converted: %s" % dest_name)
except EOFError:
print("End of file error occurred.")
except Exception:
print("Some error occurred.")
Also as #MadPhysicist emphasized:
"doing the check after open(..., 'wb') (as you did in your original code), will always say that the file exists because that is what open(..., 'w') does"
On top of that even if you made some other check for the necessity of gunzipping, doing it where you've put it will do the check on every line, which is completely redundant as the result will be the same for all lines (exists/not-exists).

Related

UnboundLocalError when I try to open an invalid file name

I have written a program that takes a file name and returns numbers and it works as expected when the file name is inputted correctly but when I try to purposely write an incorrect file name, instead of giving me a "error: file not found" from the exception, it will say:
def getFile(fileName):
lines = []
try:
infile = open(fileName, 'r')
if infile != None:
for line in infile:
lines.append(line)
except IOError:
print('Error: file not found.')
finally:
infile.close()
return lines
If an exception is raised by open, the local variable infile is never declared let alone assigned, so the attempt to call infile.close() in the finally block will raise an UnboundLocalError as you see here. You can 'fix' this somewhat by declaring infile with some special uninitialized value (e.g. None) and checking explicitly like so:
def getFile(fileName):
lines = []
infile = None
try:
infile = open(fileName, 'r')
for line in infile:
lines.append(line)
except IOError:
print('Error: file not found.')
finally:
if infile is not None:
infile.close()
return lines
Alternatively, since file objects are context managers, you can write something like:
def getFile(fileName):
lines = []
try:
with open(fileName, 'r') as infile:
for line in infile:
lines.append(line)
except IOError:
print('Error: file not found.')
return lines
... which will ensure infile is closed in a more syntactically concise & structured manner.
Note that on failure open raises an OSError (e.g. a FileNotFoundError) rather than returning None, so your existing check is redundant.
Additionally, an IOError might be raised when iterating over the file rather than opening it initially, so the error message printed may be incorrect in those circumstances.
Lastly, since infile is an iterable, you can iteratively construct a list from it readily using the constructor that accepts an iterable directly like so:
return list(infile)

Python - test if a file has already been open

I would testing if a file has already been open before writing.
Here my code :
with open(file_five, 'w') as f:
f.write(xml)
I would something as this code example :
if "file_five has already been open"
with open(file_five, 'w') as f:
f.write(xml)
else:
...
There are two ways:
1-> For Excel specific
try:
myfile = open("file_five.csv", "r+") # or "a+", whatever you need
except IOError:
print "Could not open file! !"
with myfile:
do_stuff()
2 -> For any file (Rename approach)
import os
try:
os.rename('file.xls', 'tempfile.xls')
os.rename('tempfile.xls', 'file.xls')
except OSError:
print('File is still open.')

Why doesn't "try, except" work with classic "open(fname, 'r')" in python?

I have a function that opens a file and returns an opened file object.
def read_any():
try:
opened = gzip.open(fname, 'r')
except IOError:
opened = open(fname, 'r')
return opened
When I attempt to run this function on some non-zipped file except condition does not get triggered and the function crashes with the message: IOError: Not a gzipped file.
Ok, now I try and do the same with with statement:
def read_any2():
try:
with gzip.open(fname, 'r') as f:
return f.read()
except IOError:
with open(fname, 'r') as f:
return f.read()
Now, if I try to run the same file the function works as intended.
Can you explain why doesn't except condition get triggered?
To see what's going on, test it in a REPL:
>>> import gzip
>>> f = gzip.open('some_nongzipped_file', 'r')
You will see that this doesn't raise an error. Once you, however, read from the object:
>>> f.read()
... (snip)
OSError: Not a gzipped file
, it raises the error.
In short: Simply creating the file object doesn't read anything from the file yet, and thus doesn't know if it should fail or not.
Since in the first example you just return the file object, when you try to read from it later it will raise the exception there (outside your raise-except block). In your second example you return f.read() which reads and therefore raises the exception. It has nothing to do with the with block, as you can see if you remove it:
def read_any_mod():
try:
opened = gzip.open(fname, 'r')
return opened.read()
except IOError:
opened = open(fname, 'r')
return opened.read()

Python: Readline returns error after 10 lines

I'm trying to use readline on file in a for loop. The problem is that I start getting I/O errors. It seems that I get I/O error after 10 readlines.
Here is my function:
def getAll():
with open("nodes2.txt", "r+") as f:
for i in range(0, 200):
print "**%s**"%(i)
try:
file = f.readline()
file = file[:-1]
# print "*%s*" % (file)
entities = getAllPagesEntities(file)
# print entities
for en in entities:
try:
dict = getFirmAttributes(en)
printToFile(dict)
except Exception,e:
with open("log_getFirmAttributes.txt","a") as f:
f.write(str(e))
f.write("\n")
except Exception,e:
with open("log_readFile.txt","a") as f:
f.write(str(e))
f.write("\n")
Here is a printed catched exception:
I/O operation on closed file
I think that this problem can't be caused by another used functions so I don't attach them here. I thought that it is caused by the file used but when I try to readline 200 and print them, everything works perfect.
with open("nodes2.txt", "r+") as f:
for i in range(0, 200):
print f.readline()
Have you any idea what could be the problem? Thanks
Following lines in the except block overwrites f causing open file to be closed.
with open("log_readFile.txt","a") as f:
f.write(str(e))
f.write("\n")
Change the name f for the file for appending to another name will solve the problem:
with open("log_readFile.txt", "a") as logf:
logf.write(str(e))
logf.write("\n")

Python file copy exception catching

I have a txt file called test.txt with 4 lines in it. I want to copy lines containing the word 'exception' into a new file from command line argument. I have managed this far. But I also want to exception handle this for IO error. That is if from the command line, somebody misspells the word test.txt, it will throw an exception. This is my current code. Please help! I'm a beginner. Presently, if I misspell it intentionally, it is not showing the error message I intend it to show.
import sys
def Cat(filename):
try:
f = open(filename, 'rU')
for line in f:
print (line),
return 3
except IOError:
print('\nIO error!!', filename)
def main():
f1 = open(sys.argv[1])
f2 = open(sys.argv[2], 'w')
for line in f1:
if 'exception' in line:
f2.write(line)
if __name__ == '__main__':
main()
First check if source file exists and readable -
if not (os.path.exists(file1) and os.access(file1, os.R_OK)):
print "file1 does not exists or not readable"
sys.exit(1)
else:
//good to go
and then handle writing of destination file with try except block.
You need to put the open() inside a try-except block, just as you did in Cat().
Currently, you are not calling Cat(), and the open() in main() is not inside a try-except block.
Since you said you are a beginner in Python I'll assume this a sort of "learning code". So, I won't tell you anything about the design. Also, you should do what #NPE says too.
You can try this in your main function in order to reach your goal:
def main():
filename = open(sys.argv[1])
if filename != "test.txt":
raise Exception("Here goes the message error you want to show")
f2 = open(sys.argv[2], 'w')
for line in f1:
if 'exception' in line:
f2.write(line)
You forgot to call Cat()
before
f2 = open(sys.argv[2], 'w')
Cat(f1)
for line in f1:
and in the Cat function you will need to raise exception to stop the execution
print('\nIO error!!', filename)
raise IOError('Invalid filename')

Categories

Resources