-2
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. 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. I'm not sure why with the above path (adding the /Desktop/myFolder) does not work. I am using a MacBook btw

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. Somehow when I set the path as /Users/ken/Downloads it prints nothing out as well. 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

Edit: It works when I tried it on another text editor (PyCharm). Previously I tried it on mu-editor. Idk why it doesn't work

Ken
  • 9
  • 1
  • The path is absolute, it should start from root: `r"C:\Users\ken\Desktop\myFolder\"` Edit: slashes going the other way can also be used `"C:/Users/ken/Desktop/myFolder"` – AAAlex123 Sep 13 '20 at 07:02
  • I'm using a mac, so I don't have to include C: in front isn't it? – Ken Sep 13 '20 at 07:04
  • If this code prints nothing at all, then `/Users/ken/Desktop/myFolder/` must not exist. – John Gordon Sep 13 '20 at 07:05
  • 1
    @AAAlex123 He said `/Users/ken/` worked, so I doubt that is the issue. – John Gordon Sep 13 '20 at 07:07
  • I am absolutely certain '/Users/ken/Desktop/myFolder/' exists, I created this folder just to test this code out – Ken Sep 13 '20 at 07:14
  • 1
    Does it actually print `Downloads` and `Desktop` when you try at `'/Users/ken/' `? I don't have mac but chances are they are just shortcuts. – Natthaphon Hongcharoen Sep 13 '20 at 07:16
  • Hm i included an if statement to only print out `if folderName == 'Desktop' or folderName == 'Downloads'` and commented out the other 2 for loops to only print out the root. Unfortunately no it does not print anything out. How would I go about this if I wish to access a folder on my desktop then? I tried using the same path to, for example, `os.makedirs('/Users/ken/Desktop/newFolder') and it does work in this sense though? – Ken Sep 13 '20 at 07:21
  • @NatthaphonHongcharoen yeah I think that's it. For some reason no error is printed when the path is not found. If you try `os.chdir("/Users/ken/Desktop/myFolder/")` you should see `FileNotFoundError` – AAAlex123 Sep 13 '20 at 07:23
  • Can you start terminal from file explorer? Then run something like `pwd` should prints out what your directory actually named – Natthaphon Hongcharoen Sep 13 '20 at 07:26
  • From terminal, I am able to do `cd /Users/ken/Desktop/myFolder`. That should mean that the path does exist right? – Ken Sep 13 '20 at 07:30
  • And pwd there gives you `/Users/ken/Desktop/myFolder`? – Natthaphon Hongcharoen Sep 13 '20 at 07:31
  • @NatthaphonHongcharoen yeah it does! – Ken Sep 13 '20 at 07:31
  • `print(os.listdir('/Users/ken'))` should gives you all the names possible. Maybe python just read it differently? – Natthaphon Hongcharoen Sep 13 '20 at 07:33
  • @NatthaphonHongcharoen when I do that, I do see both the directories Desktop and Downloads . Still not sure why my code doesn't work – Ken Sep 13 '20 at 07:36
  • Wait, is the `myFolder` empty? – Natthaphon Hongcharoen Sep 13 '20 at 07:39
  • And does `os.listdir('/Users/ken/Desktop')` work? – Natthaphon Hongcharoen Sep 13 '20 at 07:40
  • @NatthaphonHongcharoen In the root folder myFolder, I have another subfolder and a random text file. In the subfolder, I have another text file too. And yes `os.listdir('/Users/ken/Desktop')` does work, I can see myFolder in the list too. This is so weird – Ken Sep 13 '20 at 07:42
  • Ok I actually tried it on another text editor (PyCharm) and it works now. Idk why it doesn't work on the initial editor I used (mu-editor) – Ken Sep 13 '20 at 07:47

1 Answers1

-1

well you can use other functions such as lsdir and chdir; they are flexible to use.

sadbro
  • 112
  • 7