简体   繁体   English

os.walk() 不打印文件和文件夹名称

[英]os.walk() does not print out file and folder names

import os
mypath = '/Users/ken/Desktop/myFolder/'

for folderName, subfolders, filenames in os.walk(mypath): 
    print('The current folder is ' + folderName)
    
    for subfolder in subfolders:
        print('SUBFOLDER OF ' + folderName + ': ' + subfolder)
        
    for filename in filenames:
        print('FILE INSIDE ' + folderName + ': '+ filename)
        
print('')

Hi guys, somehow my code above, which utilises the os.walk() function, does not print out anything at all.大家好,不知何故我上面的代码,它利用了 os.walk() 函数,根本没有打印出任何东西。 I'm pretty sure this is the correct way to state the path to my folder (which is indeed located on my desktop).我很确定这是说明我的文件夹路径的正确方法(它确实位于我的桌面上)。 However, when I tried to use the path '/Users/ken/' it does return and print out a whole bunch of files/folders.但是,当我尝试使用路径“/Users/ken/”时,它会返回并打印出一大堆文件/文件夹。 I'm not sure why with the above path (adding the /Desktop/myFolder) does not work.我不确定为什么上面的路径(添加 /Desktop/myFolder)不起作用。 I am using a MacBook btw顺便说一句,我正在使用 MacBook

Edit: I realised something else odd - in my home directory I have a Downloads folder as well which stores all my recent downloads from online.编辑:我意识到还有一些奇怪的事情 - 在我的主目录中,我还有一个 Downloads 文件夹,它存储了我最近从网上下载的所有内容。 Somehow when I set the path as /Users/ken/Downloads it prints nothing out as well.不知何故,当我将路径设置为/Users/ken/Downloads它也不会打印任何内容。 It does work for any other /Users/ken/anyfolder contained in my home directory though but I'm not sure why it doesn't access my folders on my Desktop它确实适用于我的主目录中包含的任何其他/Users/ken/anyfolder但我不确定为什么它不能访问我桌面上的文件夹

Edit: It works when I tried it on another text editor (PyCharm).编辑:当我在另一个文本编辑器(PyCharm)上尝试它时它有效。 Previously I tried it on mu-editor.以前我在 mu-editor 上试过。 Idk why it doesn't work知道为什么它不起作用

well you can use other functions such as lsdir and chdir;好吧,您可以使用其他功能,例如 lsdir 和 chdir; they are flexible to use.它们使用起来很灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM