I am trying to read a value from a file "build_ver.txt" on a network location,increment the value by one and write back the incremented new value to "build_ver.txt" and running into following error,can anyone provide suggetions on how to achieve this?
with open('\\network\files\build_ver.txt','w+') as f:
value = f
value = value+1
value_inc = open('\\network\files\build_ver.txt','w+')
value_inc.write(value)
ERROR:-
Traceback (most recent call last):
File "build_ver.py", line 1, in <module>
with open('\\network\files\build_ver.txt','w+') as f:
IOError: [Errno 22] invalid mode ('w+') or filename: '\\network\x0ciles\x08uild_ver.txt'
Backslashes escape special Characters, in your case '\\f': form-feed and '\\b': bell. You have to escape the backslash with another backslash or use the r''-Syntax. Next problem: you don't read the file, you only rename the object. If you would read the object, you would have a string and not a number, and if you would convert the string to a number you couldn't write it because it's not a string. All in all you get this:
with open(r'\\network\files\build_ver.txt','r+') as f:
value = int(f.read())
f.seek(0)
f.write(str(value + 1))
In python backslashes are used as escape sequences. Here is what happens if I type in your string:
>>> '\\network\files\build_ver.txt'
'\\network\x0ciles\x08uild_ver.txt'
>>> print '\\network\files\build_ver.txt'
\network
ileuild_ver.txt
>>>
Instead, change your code to:
value_inc = open('\\network\\files\\build_ver.txt','w+')
value_inc.write(value)
As shown:
>>> print '\\network\\files\\build_ver.txt'
\network\files\build_ver.txt
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.