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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With