weixin_43732226 2019-05-13 20:46 采纳率: 82.1%
浏览 1950
已采纳

with open写入数据的时候 是只能创建最后一个文件吗

with open( '/path/to/file', 'r' ) as f:
print( f.read() )

比如说这段代码里面,如果path,to这两个文件夹不存在的话,就会显示无法创建文件夹嘛?不会自动创建的是吧?

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-05-13 23:56
    关注
    if not os.path.exists( '/path/to/'):
          os.makedirs(path)
    with open( '/path/to/file', 'a' ) as f:
    print( f.read() )
    
    注意,,a这个参数就是自动创建
    
    

    https://blog.csdn.net/Fantasy_Virgo/article/details/82315727

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 1月16日