# the use of "open" in digi

**URL:** <https://forums.digi.com/t/the-use-of-open-in-digi/2830>\
**Category:** Python\
**Created:** [May 26, 2012, 8:52am UTC](https://forums.digi.com/t/the-use-of-open-in-digi/2830 "2012-05-26T08:52:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![songsong](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@songsong](https://forums.digi.com/u/songsong)\
**Post date:** [May 26, 2012, 8:52am UTC](https://forums.digi.com/t/the-use-of-open-in-digi/2830/1 "2012-05-26T08:52:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kurte](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@kurte](https://forums.digi.com/u/kurte)\
**Post date:** [May 28, 2012, 8:03pm UTC](https://forums.digi.com/t/the-use-of-open-in-digi/2830/2 "2012-05-28T20:03:44Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![lynnl](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@lynnl](https://forums.digi.com/u/lynnl)\
**Post date:** [May 29, 2012, 3:25pm UTC](https://forums.digi.com/t/the-use-of-open-in-digi/2830/3 "2012-05-29T15:25:36Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![nathanf](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/nathanf/32/5307_2.png) [@nathanf](https://forums.digi.com/u/nathanf)\
**Post date:** [September 28, 2012, 11:01pm UTC](https://forums.digi.com/t/the-use-of-open-in-digi/2830/4 "2012-09-28T23:01:11Z")

</div>

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”)  
self.\_output.close()  
else:  
print “creating new”  
self.\_output = open(self.filename, “w”)  
str2write = “Default settings in here.”  
self.\_output.write(str2write)  
self.\_output.close()

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.
