the use of "open" in digi

where I type
" logfile = open(a.txt’, ‘w’) " and run in my pc ,it is allright
but,when it run in digi connectPort X4,
will " IOError: [Errno 19] No such device: ‘a.txt’ "
I do not why? Thanks,help me

You may have to specify the directory. For example this should work:

open(‘WEB/a.txt’, ‘w’)

Kurt is correct - some products (like the X2/X4) have no ability to ‘change’ directories, so ‘a.txt’ would need to exist in the root of a read-only file-system you don’t have access to.

On the X2/X4 (& other connect products)

  • ‘WEB/a.txt’ would exist in the area you access with the web UI under the Administration ‘file management’.
  • ‘WEB/python/a.txt’ would exist in the area you access with the web UI under the Applications ‘Python’.

Also, it’s always a good idea to wrap file/io type operations in a try/catch block. This way, you can do things such as try to open a file to read, and if it’s not there, catch the exception and create a new file. The other option is to use the ‘if file exists’ functionality, that way you can take care of instances where if used for soemthing such as a configuration file for a script and the file was accidentally erased, you can have a clause which will create a new file with some default settings. For instance, something like the following:
if (os.path.exists(self.filename)):
print “file exists”
self._output = open(self.filename, “a”)
print “creating new”
self._output = open(self.filename, “w”)
str2write = “Default settings in here.”

This was taken from a file logger class I created, so you may not be able to use it line for line, but it should give you an idea as to how to react and handle cases where the file is not there and handling gracefully, instead of causing an unhandled exception.