Remove a file in Python - python

I'm trying to remove a file that physically exists on my system. I wrote the following function using Python 3 that does not work:
def remove_file(output_file):
print("Removing the output file: ", output_file)
try:
os.remove(output_file)
except RemoveFileError as e:
remove_stat = e.returncode
else:
remove_stat = 1
if remove_stat == 0:
print("File removed!")
else:
print("File not removed.")
When I try to remove the file using the above code this is the result I get:
Removing the output file: ../../../output_files/aws_instance_list/aws-master-list-03-21-2019.csv
File not removed.
What am I doing wrong?

The else clause of your try statement is being executed when no error occurred. See Handling Exceptions
Try this updated version of your code:
def remove_file(output_file):
print("Removing the output file: ", output_file)
try:
os.remove(output_file)
except RemoveFileError as e:
remove_stat = e.returncode
else:
remove_stat = 0
if remove_stat == 0:
print("File removed!")
else:
print("File not removed.")
print("File exists:", os.path.exists(output_file))

Related

Using .hdf5 files only once they are finished writing

I am trying to use .hdf5 files once they are done writing (in my case, trying to emit them). But the problem is that I don't have a way to 1) test if they are finished writing and 2) then send them. The code that I have been trying to work with is follows:
while True:
event = self._q.get()
while True:
try:
file = h5py.File(event.src_path, "r")
file.close()
self.new_file.emit(event.src_path, os.path.basename(event.src_path))
break
except OSError:
if retry_count < max_retry_count:
retry_count += 1
print(f"h5 file <{event.src_path}> is locked, retrying {retry_count}/{max_retry_count}")
time.sleep(retry_interval_seconds)
else:
print(f"h5 file <{event.src_path}> reached max retry count, skipping")
except Exception as err:
print(f"Got unexpected Error <{type(err).__name__}> while opening <{event.src_path}> ")
traceback.print_exc()
Obviously this is problematic with the break. But without the break, the try stays in the loop and emits the same file over and over again. This code tests if they are done writing perfectly but the ability to send them and continue to take in new files does not work. Any insight is greatly appreciated.
I solved this by the following code:
while True:
event = self._q.get()
max_retry_count = 350 # for test purposes now but want to set an upper bound on verifying a file is finished.
retry_interval_seconds = .01 # every hundreth it will try the file to see if it finished writing
retry_count = 0
if event.event_type == "created" and event.src_path.lower().endswith(".hdf5"):
while True:
try:
file = h5py.File(event.src_path, "r")
file.close()
except OSError:
if retry_count < max_retry_count:
retry_count += 1
print(f"h5 file <{event.src_path}> is locked, retrying {retry_count}/{max_retry_count}")
time.sleep(retry_interval_seconds)
else:
print(f"h5 file <{event.src_path}> reached max retry count, skipping")
break # <--- looks useful here
except Exception as err:
print(f"Got unexpected Error <{type(err).__name__}> while opening <{event.src_path}> ")
traceback.print_exc()
else:
self.new_file.emit(event.src_path, os.path.basename(event.src_path))
break

How to Continue Loop after exception

from textblob import TextBlob
line = """ अशा किंमतीवर खरोखरच चांगला कुकर आहे ok are you sure how are you आधुनिक तंत्रज्ञानासह हे सुलभ आणि सुरक्षित are you आहे இது அற்புதமான தரம் மற்றும் சூப்பர் தயாரிப்பு."""
def split_line(in_line):
line_sp = line.split(" ")
line_two = [" ".join(line_sp[i:i + 3]) for i in range(0, len(line_sp), 3)]
return line_two
#print(split_line(line))
try:
for i in split_line(line):
blob = TextBlob(i)
print (blob.translate(to = 'en'))
except:
print ("same language found not translated")
This is language Translation code some times text blob throw error so I used try/except block so my code stops when print error message but I want to continue this loop after catching exception
Just put the try except block inside the for (if the error is raised by TextBlob(i)):
for i in split_line(line):
try:
blob = TextBlob(i)
print (blob.translate(to = 'en'))
except:
print ("same language found not translated")
This will make the for run until it ends even if an error has been raised in it.

Making so that my get python-requests are faster [duplicate]

This question already has answers here:
What is the fastest way to send 100,000 HTTP requests in Python?
(21 answers)
Closed 6 years ago.
I have a python-script with a lot of exceptions. I'm trying to make around 50,000 requests. And it is very slow as of now also I'd like for my script to be running therefore I added almost all the exceptions request has which has mostly to do with connectionError etc.
Is there a way I can make this script so it's much faster than it is now and more modular?
for i in range(50450000,50500000):
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
check_response = 'http://www.barneys.com/product/adidas--22human-race-22-nmd-sneakers-'+str(i)+'.html'
make_requests = requests.get(check_response,headers=headers).text
soup = BeautifulSoup(make_requests)
try:
main_wrapper = soup.find('h1',attrs={'class':'title'}).text
print main_wrapper + ' ' + str(i)
except AttributeError:
arr.append(check_response)
with open('working_urls.json','wb') as outfile:
json.dump(arr,outfile,indent=4)
except requests.exceptions.InvalidURL:
continue
except requests.exceptions.InvalidSchema:
continue
except requests.exceptions.MissingSchema:
continue
except requests.exceptions.TooManyRedirects:
continue
except requests.exceptions.URLRequired:
continue
except requests.exceptions.ConnectTimeout:
continue
except requests.exceptions.Timeout:
continue
except requests.exceptions.SSLError:
continue
except requests.exceptions.ProxyError:
continue
except requests.exceptions.HTTPError:
continue
except requests.exceptions.ReadTimeout:
continue
except requests.exceptions.ConnectionError:
continue
First, please replace all these ugly try/except blocks by a single one, like:
for i in range(50450000,50500000):
try:
check_response = 'http://www.barneys.com/product/adidas--22human-race-22-nmd-sneakers-'+str(i)+'.html'
make_requests = requests.get(check_response,headers=headers).text
soup = BeautifulSoup(make_requests)
try:
main_wrapper = soup.find('h1',attrs={'class':'title'}).text
print main_wrapper + ' ' + str(i)
except AttributeError:
arr.append(check_response)
with open('working_urls.json','wb') as outfile:
json.dump(arr,outfile,indent=4)
except requests.exceptions.InvalidURL:
continue
except requests.exceptions.InvalidSchema:
continue
except requests.exceptions.MissingSchema:
continue
...
And if everything you do is continue in all cases, use the base class RequestException. It becomes:
try:
check_response = 'http://www.barneys.com/product/adidas--22human-race-22-nmd-sneakers-'+str(i)+'.html'
make_requests = requests.get(check_response,headers=headers).text
soup = BeautifulSoup(make_requests)
try:
main_wrapper = soup.find('h1',attrs={'class':'title'}).text
print main_wrapper + ' ' + str(i)
except AttributeError:
arr.append(check_response)
with open('working_urls.json','wb') as outfile:
json.dump(arr,outfile,indent=4)
except requests.exceptions.RequestException:
pass
Maybe not faster, but for sure far easier to read!
As for the speed issue, you should consider using threads/processes. Take a look at the threading and multiprocessing modules.

Why isn't my python script working? [duplicate]

This question already has answers here:
IndentationError: unindent does not match any outer indentation level
(32 answers)
Closed 6 years ago.
I keep on getting the same error:
File "backup.py", line 26
logging.error("Unable to create backup.zip")
IndentationError: unindent does not match any outer indentation level
This is my script:
import sys
import os
import logging
logging.basicConfig(filename='file_ex.log', level = logging.DEBUG)
logging.info("checking to see if the backup.zip exists")
if os.path.exists("backup.zip"):
logging.info("It exists!")
try:
zip_file = zipfile.ZipFile('backup.zip','a')
except:
err = sys.exc_info()
logging.error("Unable to open backup.zip in append mode")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " = err[1].args[1])
sys.exit()
else:
logging.info("Creating backup.zip")
try:
zip_file = zipfile.ZipFile('backup.zip', 'w')
except:
err = sys.exc_info()
logging.error("Unable to create backup.zip")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " + err[1].args[1])
sys.exit()
else:
logging.info("Creating backup.zip")
try:
zip_file = zipfile.ZipFile('backup.zip', 'w')
except:
err = sys.exc_info()
logging.error("Unable to create backup.zip")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " + err[1].args[1])
logging.info("adding test.txt to backup.zip")
try:
zip_file.write('test.txt', 'test.txt', zipfile.ZIP_DEFLATED)
except:
err = sys.exc_info()
logging.error("Unable to open backup.zip in append mode")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " = err[1].args[1])
zip_file.close()
You have errors on line 17 and 48. logging.error("Error Msg: " = err[1].args[1])
Try fix your indentation.
It is hard from your example to see exactly where your try except statements belong but I have done a code snippet with what I believe would be correct indentation based on what I believe you are trying to do.
import sys
import os
import logging
logging.basicConfig(filename='file_ex.log', level = logging.DEBUG)
logging.info("checking to see if the backup.zip exists")
if os.path.exists("backup.zip"):
logging.info("It exists!")
try:
zip_file = zipfile.ZipFile('backup.zip','a')
except:
err = sys.exc_info()
logging.error("Unable to open backup.zip in append mode")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " = err[1].args[1])
sys.exit()
else:
logging.info("Creating backup.zip")
try:
zip_file = zipfile.ZipFile('backup.zip', 'w')
except:
err = sys.exc_info()
logging.error("Unable to create backup.zip")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " + err[1].args[1])
sys.exit()
else:
logging.info("Creating backup.zip")
try:
zip_file = zipfile.ZipFile('backup.zip', 'w')
except:
err = sys.exc_info()
logging.error("Unable to create backup.zip")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " + err[1].args[1])
logging.info("adding test.txt to backup.zip")
try:
zip_file.write('test.txt', 'test.txt', zipfile.ZIP_DEFLATED)
except:
err = sys.exc_info()
logging.error("Unable to open backup.zip in append mode")
logging.error("Error Num: " + str(err[1].args[0]))
logging.error("Error Msg: " = err[1].args[1])
zip_file.close()

String indices must be integers in one place of code in python

import pymongo
import sys
client=pymongo.MongoClient('localhost',27017)
db=client.rop
try:
cntcur=db.albums.aggregate([{"$unwind":"$images"},"$group":"_id":"null","count":{'$sum':1}}}])
cursor1=db.images.find()
for im in cursor1:
id1=int(im['_id'])
cnt=0
cursor= db.albums.aggregate([{"$unwind":"$images"}])
print id1
for image in cursor:
print "moving to images collection"
ig=image['images']
if (id1 == ig):
break;
else:
cnt=cnt+1
if (cnt == cntr):
print "removing"
db.images.remove({'_id':id1})
except Exception as e:
print "unexpected error", type(e),e
I get error in the line ig=image['images']. I tried changing it to ig=int(image['images']), but I get same error.
import pymongo
import sys
client = pymongo.MongoClient('localhost',27017)
db=client.rop
try:
cursor1=db.images.find()
for im in cursor:
id1=int(im['_id'])
print id1
cnt=0
cursor=db.albums.find()
for image in cursor:
ig=image['images']
for data in ig:
if (id1 == data):
cnt=1
break;
if cnt==0:
print "removing"
db.images.remove({'_id':id1})
except Exception as e:
print "unexpected error",type(e) , e

Categories