Join us at IRC!
Become the change you seek in the world. - Gandhi
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 32
Web Spiders: 15
Guests Online: 28
Members Online: 4

Registered Members: 70179
Newest Member: prankst3r
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Exiting Python?

shadowboy1505
Member

Posts: 24
Location:
Joined: 12.02.08
Rank:
Mad User
Posted on 08-12-10 00:55
Okay so I have a program that I've written in Python 3. It creates a file log and writes it to a file. If there are no logs, the file doesn't get created. Later in the program it calls the file, but the file doesn't exist. I want it to catch the exception when it can't open the file and just exit the scblockedript and do nothing else but I can't figure out how. The exit and quit commands don't do what I want. I've tried to Google it but I can't find anything on it. Can someone lead me in the right direction please?
Author

RE: Exiting Python?

skathgh420
Member



Posts: 418
Location: 127.0.0.1
Joined: 03.03.08
Rank:
God
Posted on 08-12-10 03:34
os.path.isfile(\path\to\file)


os.path.exists(\path\to\file)


1 3 3 www.google.com
Author

RE: Exiting Python?

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 08-12-10 03:36
Try this:
#!/usr/bin/python
import sys
for x in range(1, 300):
print x
if(x==20):
sys.exit(0)






ynori7 http://halls-of-valhalla.org
Author

RE: Exiting Python?

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 08-12-10 17:19
#!/usr/bin/python
try:
open('File that doesnt exist', 'r')
except IOError:
print "Can't open file."
exit(1)




and a quick google
import sys

try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise


From http://docs.python.org/tutorial/errors.html


BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.




Edited by wolfmankurd on 08-12-10 17:24
Widowmakr@hotmail.com http://LetsHackStuff.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.