Write file to a directory that doesn't exist

后端 未结 4 1493
[愿得一人]
[愿得一人] 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:10

    Make liberal use of the os module:

    import os
    
    if not os.path.isdir('/Users/bill/output'):
        os.mkdir('/Users/bill/output')
    
    with open('/Users/bill/output/output-text.txt', 'w') as file_to_write:
        file_to_write.write("{}\n".format(result))
    

提交回复
热议问题