I am developing on a Windows machine some code that will be run in a Linux environment. I know the path of the file in Linux will be /tmp/ that I need to write to. The accepted answer to this question does not work on Windows: Platform-independent file paths?
when I try open(path.join('tmp', 'fileToOpen.txt'), 'w')
I get "FileNotFoundError: [Error 2] No such file or directory: 'tmp\fileToWrite.txt'"
How can this be resolved?
As @Tomerikoo and @jakub pointed out, you want to use the pathlib
and tempfile
modules in Python:
tempfile.gettempdir()
will retrieve the location and pathlib.PurePath
will join the filename to that path in an OS agnostic fashion. You'll end up with something like:
open(PurePath(gettempdir(), 'fileToOpen.txt'), 'w')
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.