Hi,
sorry for my poor title description.
I am rather new to python and programming, but read some tutorials and books about simple string manipulations and file handly already.
Pre-infomation: I know, I have a rather old version of python (3.7.9), because it came with Thonny (3.3.13) and I do not know, if it would work, if I just would copy a newer into the Thonny directory.
Further I run this (thonny) on a Windows 10. (European keyboard code page [German, codepage 1252])
(Can't just test my code on Linux or Mac)
My problem is, that I get error messages, when trying to save to another/ subdirectory, when opened for reading another, before. (writing to new files in same directory with new file name or to script.py-projects-folder ("bla.txt") works normal)
Also one time I found another message: OSError: [Errno 22] Invalid argument: 'D:\\MOC\\extraced\LowerDir\\Doc.txt' - which even does not fit to my code g = open ("D:\MOC\extraced\LowerDir\Doc.txt", "w") above.
OK, I know, python or Windows could not open such file path. But why does it alternate those strings in such a strange way by itself?
Annotation: I copied the error messages by clipboard from Thonny - Shell window (from Windows terminal console are the same). The single or double backslashes in my python code and the output messages are exactly copied form the error messages and not altered!
I have 'D:\other\Doc.txt' in my code, and python answers with 'D:\other\\Doc.txt', which logically fails.
My code:
But saving to different (sub)folders should work with older versions, too -doesn't it?.
Saving into the same directory with other filename or creating new files witout reading before just works fine.
Also I guess I typed the backslashes correctly in my code, because saving to different filenames works D:\bla\fname2.txt and I only added a subDirName between them. D:\bla\blub\fname.txt
How can python internally change the path "C:\bla\blubb\file.txt" to 'C:\\bla\blubb\\file.txt' ???
Only saving to my python code files directory (without folder names, e.g. f= open("HelloThere.txt", "w") works either without problems and prpperly gerenrates files in this project folder.) (and saving to "opened before" directories, like above)
What do I wrong? (windows code page is set, no foreign characters in filenames and folder names; backslashes as character 0x5C ). I have no further clue.
Thank you!
sorry for my poor title description.
I am rather new to python and programming, but read some tutorials and books about simple string manipulations and file handly already.
Pre-infomation: I know, I have a rather old version of python (3.7.9), because it came with Thonny (3.3.13) and I do not know, if it would work, if I just would copy a newer into the Thonny directory.
Further I run this (thonny) on a Windows 10. (European keyboard code page [German, codepage 1252])
(Can't just test my code on Linux or Mac)
My problem is, that I get error messages, when trying to save to another/ subdirectory, when opened for reading another, before. (writing to new files in same directory with new file name or to script.py-projects-folder ("bla.txt") works normal)
>>> %Run check.py Traceback (most recent call last): File "C:\Users\Alex\Documents\PYT\check.py", line 4, in <module> g = open ("D:\MOC\extraced\LowerDir\Doc.txt", "w") OSError: [Errno 22] Invalid argument: '"D:\MOC\extraced\LowerDir\\Doc.txt'bytheway: alternate saving directory: OSError: [Errno 22] Invalid argument: 'D:\otherFolder\\Doc.txt'
Also one time I found another message: OSError: [Errno 22] Invalid argument: 'D:\\MOC\\extraced\LowerDir\\Doc.txt' - which even does not fit to my code g = open ("D:\MOC\extraced\LowerDir\Doc.txt", "w") above.
OK, I know, python or Windows could not open such file path. But why does it alternate those strings in such a strange way by itself?
Annotation: I copied the error messages by clipboard from Thonny - Shell window (from Windows terminal console are the same). The single or double backslashes in my python code and the output messages are exactly copied form the error messages and not altered!
I have 'D:\other\Doc.txt' in my code, and python answers with 'D:\other\\Doc.txt', which logically fails.
My code:
f = open ("D:\MOC\extraced\Document.txt", "r") # g = open ("D:\MOC\extraced\Document_3_bla.txt", "w") only # this code works properly # g = open ("D:\MOC\extraced\LowerDir\Doc.txt", "w") # this does not work - even if subDir exists or not g = open ("C:\anyOtherPathOnMyHDD\outputfile.txt", "w") # this does not work either. (C:\bla is not restricted and it does not matter if C:\path does exist before or not. Also other Partitons D:, E:, G: do not work better. It seems to be the different path from opening) text = f.read() newtext = bla(text) # string alternation in functions g.write (newtext) g.flush() # do not know, if really necessary g.close() f.close()OK, there is my old version of python.
But saving to different (sub)folders should work with older versions, too -doesn't it?.
Saving into the same directory with other filename or creating new files witout reading before just works fine.
Also I guess I typed the backslashes correctly in my code, because saving to different filenames works D:\bla\fname2.txt and I only added a subDirName between them. D:\bla\blub\fname.txt
How can python internally change the path "C:\bla\blubb\file.txt" to 'C:\\bla\blubb\\file.txt' ???
Only saving to my python code files directory (without folder names, e.g. f= open("HelloThere.txt", "w") works either without problems and prpperly gerenrates files in this project folder.) (and saving to "opened before" directories, like above)
What do I wrong? (windows code page is set, no foreign characters in filenames and folder names; backslashes as character 0x5C ). I have no further clue.
Thank you!