Python Forum
does not save in other path than opened files before
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does not save in other path than opened files before
#1
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)

>>> %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!
Reply


Messages In This Thread
does not save in other path than opened files before - by icode - Jun-23-2023, 03:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 681 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 1,176 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  How to save files in a separate directory Scordomaniac 3 2,783 Mar-16-2022, 10:17 AM
Last Post: Gribouillis
Question How to get html information from a tab of my default browser opened with webbrowser? noahverner1995 2 4,831 Jan-14-2022, 10:02 AM
Last Post: noahverner1995
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,347 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  get all the files in the path in a list? korenron 23 7,794 Jul-19-2021, 07:44 AM
Last Post: korenron
  Rmarkdown opened by python code - errors Rav013 0 2,206 Apr-27-2021, 03:13 PM
Last Post: Rav013
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 4,151 Jan-18-2020, 07:12 PM
Last Post: SriRajesh
  Downloading And Saving Zip Files To A Particular Path Folder eddywinch82 2 2,694 Jan-06-2020, 07:56 PM
Last Post: eddywinch82
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 5,910 Nov-12-2019, 05:59 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020