Write file to a directory that doesn't exist

后端 未结 4 1492
[愿得一人]
[愿得一人] 2020-12-14 02:35

How do I using with open() as f: ... to write the file in a directory that doesn\'t exist.

For example:



        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 03:02

    For Python 3 can use with pathlib.Path:

    from pathlib import Path
    
    p = Path('Users' / 'bill' / 'output')
    p.mkdir(exist_ok=True)
    (p / 'output-text.txt').open('w').write(...)
    

提交回复
热议问题