python3 os运行后如何保存txt格式的文件

665次阅读
没有评论

python3

存储物品在动物界中是不同的,松鼠喜欢藏在树洞里,蚂蚁喜欢运回底下巢穴。现实工作时,如果只是一段纯文本的内容,相信很多人优先会选择txt文件的保存形式。小编的话为了省时省力,一般情况下会选择存到txt中去。当然这种方法用的不多,所以今天小编来教大家python3 os运行后保存成txt的方法。

 

目前代码:

import os
for root, dirs, files in os.walk(".", topdown=True):     
     for name in files:         
          print(os.path.join(root, name))    
     for name in dirs:         
          print(os.path.join(root, name))

 

按照这样去修改

import os
 
with open("python.txt", "w") as f:
 
    for root, dirs, files in os.walk(".", topdown=True):     
         for name in files:         
              f.write(os.path.join(root, name) + "\n")    
         for name in dirs:         
              f.write(os.path.join(root, name) + "\n")

python3

 

按照上面的代码修改之后,我们就会发现python3 os运行后可以保存成txt啦,小伙伴们写错代码的也快试试。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-27发表,共计718字。
新手QQ群:570568346,欢迎进群讨论 Python51学习