Follow us on Twitter!
Don't judge the unknown - Grindordie
Thursday, April 17, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 16
Guests Online: 16
Members Online: 0

Registered Members: 82815
Newest Member: medjiking
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Exiting Python?

shadowboy1505
Member

Your avatar

Posts: 24
Location:
Joined: 12.02.08
Rank:
Newbie
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 script 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?


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 08-12-10 03:34
os.path.isfile(\path\to\file)


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


Author

RE: Exiting Python?

ynori7
Member



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






halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Exiting Python?


Member

Your avatar

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







and a quick google
Code
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.pytho. . .rrors.html




Edited by on 08-12-10 17:24