简体   繁体   中英

How to write to a directory os agnostic?

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:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM