(Jun-23-2023, 03:49 PM)icode Wrote: Thonny (3.3.13) and I do not knowGet a new version of Thonny it's comes with Python 3.10 built in.
Do not use singel
\
in Path for Windows,because of escape characters.Here you clearly see the problem.
>>> f = "C:\anyOtherPathOnMyHDD\outputfile.txt" >>> f 'C:\x07nyOtherPathOnMyHDD\\outputfile.txt'So raw string
r
or just turn around /
work fine.>>> f = r"C:\anyOtherPathOnMyHDD\outputfile.txt" >>> f 'C:\\anyOtherPathOnMyHDD\\outputfile.txt' >>> >>> f = "C:/anyOtherPathOnMyHDD/outputfile.txt" >>> f 'C:/anyOtherPathOnMyHDD/outputfile.txt'The basic of open and write files,and use
with open
then do not need to close file.with open(r'G:\div_code\hello.txt') as fp: result = fp.read() print(result) # Add 123 to hello world with open(r'G:\div_code\new_hello.txt', 'w') as fp_out: add_numb = f'{result} 123' fp_out.write(add_numb)
Output:hello world
hello.txt:
Output:hello world
new_hello.txt:Output:hello world 123