3

Python 3 os.walk读取指定文件路径后,打印路径参数为空 - 核桃比利I

 1 year ago
source link: https://www.cnblogs.com/hetao-bili/p/17205504.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

今天有时间自己尝试了一下os.walk的小实验,结果出现了一个小问题:在交互模式下,运行我的python脚本,没有打印任何内容

2984273-20230311105957678-96674790.png

 返回去看一下test.py内容

2984273-20230311110211819-1391781959.png

返回去看一下文件路径是否正确:

2984273-20230311110758224-474928887.png

 看着好像没有任何问题。。。

然后仔细想了想发现,可能是运行的脚本test.py与想要访问的目录中的test2存在相同字段,导致python不能正确区分,然后就是验证过程:

首先将文件名称test2改为其他(ASAD)

2984273-20230311111057100-758429565.png

然后修改python文件内的路径:

2984273-20230311111207927-1030273476.png

再次在交互模式下运行:

2984273-20230311111316599-1434689882.png

哇,可以看到运行成功了,目录及子目录下的文件夹、文件都打印出来了

总结:打印失败就是因为py脚本名称与路径名称存在重复导致的。虽然是个小问题,但是很容易被忽略。尤其是类似test*这种常用名称


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK