I Was trying to run this piece of code to generate a directory and a file inside it, the thing is that both the of them won't get created until the program is terminated.
What can i do to create them while the program is still running?
def makeDirectory(dirRoot, path, fileName, fromaddr):
try:
os.mkdir(dirRoot, 0o0755)
except OSError as e:
print(e)
try:
os.mkdir(path, 0o0755)
except OSError as e:
print(e)
path = os.path.abspath(path + '/')
completeName = os.path.join(path, 'foo.txt')
cnt = open(completeName, 'a')
cnt.close()
Related
I'm trying to implement concurrent requests to speed up the checking of a list of URL's but it doesn't seem to be working with my code as it's still checking them 1 by 1.
for domain in list:
try:
follow_url = requests.head(f'http://{domain}', allow_redirects=True, timeout=60)
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
executor.submit(follow_url)
with open("alive.txt", "a") as file:
file.write(f'{domain}\n')
except Exception as e:
print(e)
You are not applying it correctly. You are creating parallel processes inside an iteration. Correct way could be like this:
def parallel_req(domain):
try:
follow_url = requests.head(f'http://{domain}', allow_redirects=True, timeout=60)
with open("alive.txt", "a") as file:
file.write(f'{domain}\n')
except requests.exceptions.RequestException as e:
print(e)
with ThreadPoolExecutor() as e:
e.map(parallel_req, domains)
I am trying to implement exception handeling using Pyspark in Databricks, where I need to check the file if it exists in the source location.
df = spark.read.csv.option("inferschema", "true").load("mnt/pnt/abc.csv")
try:
df = open("abc.csv", "rt")
print("File opened")
except FileNotFoundError:
print("File does not exist")
except:
print("Other error")**
I wish to have something like the above code snippet however i am not being able to take the approach. I would request some help would be really thankful
You can't directly Except java.io errors, however you could do something like:
def read_file(path):
try:
dbutils.fs.ls(path)
return spark.read.option("inferschema","true").csv(path)
except Exception as e:
if 'java.io.FileNotFoundException' in str(e):
print('File does not exists')
else:
print('Other error')
read_file('mnt/pnt/abc.csv')
I have this code below: (am using python 3.6.4)
def recursedir(dirname, filename):
os.chdir(dirname)
try:
# print('Searching in ' + os.getcwd())
for root, subdir, files in os.walk('.'):
for fname in files:
if fnmatch.fnmatch(fname, filename):
name = os.path.join(root, fname)
name = re.sub(r"\.\\","", name)
return name
except FileNotFoundError:
print(dirname + ' does NOT exist. SKIPPED')
return None
But it seems that the "except FileNotFoundError" does not catch the error and I still get this error message:
FileNotFoundError: [WinError 2] The system cannot find the file
specified: 'e:\workspace\Share'
The e:\workspace\Share indeed does not exist, but I just want to print out a message saying "This directory does NOT exist" and continue on.
I am attempting to read a file called "files.txt" and return each line in order. I receive and catch a no such file or directory when I'm sure the text file is in my working directory. Is there something improper about the code?
def main():
FileRead()
def ReadFile(filename):
files = open(filename)
lines = files.readlines()
for index, line in enumerate(lines):
print(index, "=", line)
def FileRead():
try:
ReadFile("files.txt")
except IOError as e:
print("Could not open file:", e)
if __name__ == "__main__":
main()
How can i recover from exception and continue line string from opened file? I'm stuck!
try:
while True:
with open('us.txt') as f:
for user in f:
for tweet in tweepy.Cursor(api.user_timeline, screen_name=user, ).items():
print(tweet.user.screen_name)
csvWriter.writerow(tweet.user.screen_name)
except tweepy.TweepError as e:
print(e.reason)
sys.exit()
If your (admittedly a bit confusing) question is asking how to ignore the exception, but still continue from where you were in the file, you should try and keep your try: and except: blocks as close to the problematic line.
For example, if csvWriter.writerow(tweet.user.screen_name) is the line which fails, you could do:
while True:
with open('us.txt') as f:
for user in f:
for tweet in tweepy.Cursor(api.user_timeline, screen_name=user,).items():
print(tweet.user.screen_name)
try:
csvWriter.writerow(tweet.user.screen_name)
except tweepy.TweepError as e:
print(e)
If instead the error is in the for tweet in tweepy.Cursor(... line, you could do this:
while True:
with open('us.txt') as f:
for user in f:
try:
for tweet in tweepy.Cursor(api.user_timeline, screen_name=user,).items():
print(tweet.user.screen_name)
csvWriter.writerow(tweet.user.screen_name)
except tweepy.TweepError as e:
print(e)
I hope this helps!