Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-21-2019, 02:46 AM
Neisany Neisany is offline
Registered User
 
Join Date: Feb 2019
Posts: 8
Error except syntax error

the file that it is trying to call does not exist.
I am looking to catch the IO error if it is present and then create the file.

Code:
def main():
	with open('C:\Users\AC30419\Documents\test.txt','r+') as f:
		except:
			crt.Diaglog.MessegeBox('error')

main()
creating the file would be as simple as
Code:
def main():
	with open('C:\Users\AC30419\Documents\test.txt','w') as f:
		pass

main()
Reply With Quote
  #2  
Old 05-21-2019, 04:12 AM
Neisany Neisany is offline
Registered User
 
Join Date: Feb 2019
Posts: 8
Okay the error in my way was not using the try command before the with statement thanks
Reply With Quote
  #3  
Old 05-21-2019, 10:41 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 919
Quote:
Originally Posted by Neisany View Post
Okay the error in my way was not using the try command before the with statement thanks
In addition, there's an issue with crt.Diaglog.MessegeBox not being spelled correctly (it's crt.Dialog, not crt.Diaglog; it's MessageBox, not MessegeBox).

Also, indentation matters in Python. Here's an example of correct indentation:
Code:
# $language = "Python"
# $interface = "1.0"

def main():
    # Open a file and read all of its contents into a variable
    import os
    strHome = os.path.expanduser("~")
    strPathToFile = "{0}/Documents/test.txt".format(strHome)
    strPathToFile = strPathToFile.replace("\\", "/")
    strContents = ""
    try:
        with open(strPathToFile, "r") as objFile:
            strContents = objFile.read()
    except Exception as objInst:
        crt.Dialog.MessageBox(
            "Error attempting to read file:\n\n{0}".format(
            str(objInst)))
        return

    # If we get here, we were able to read all of the file
    # data successfully into the variable: strContents

    crt.Dialog.MessageBox("Contents of the file:\r\n\r\n{0}".format(
        strContents))

main()
--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #4  
Old 05-23-2019, 05:38 AM
ogun ogun is offline
Registered User
 
Join Date: Sep 2013
Posts: 14
Another option as well instead of using exceptions would be to use os.path.exists to check if the file is there or not. Something like this should work:

Code:
# $language = "Python"
# $interface = "1.0"

def main():
    # Open a file and read all of its contents into a variable
    import os
    strHome = os.path.expanduser("~")
    strPathToFile = os.path.join(strHome,"/Documents/test.txt")
    if os.path.exists(strPathToFile):
        # File exists, read contents
        strContents = open(strPathToFile, "r").read()
    else:
        # File doesn't exist
        crt.Dialog.MessageBox(
            "Error attempting to read file:\n\n{0}".format(
            str(objInst)))
        return

    # If we get here, we were able to read all of the file
    # data successfully into the variable: strContents

    crt.Dialog.MessageBox("Contents of the file:\r\n\r\n{0}".format(
        strContents))

main()
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:50 PM.