pythongoogle-cloud-platformjupyter-labgoogle-cloud-ai

Can't iterate over files within a folder in google cloud notebooks instance


I am using the notebooks instance within the AI platform in google cloud console. I have uploaded a folder inside which contains around 30 csv files.

I run the following code to iterate over the files,

for subdir, dirs, files in os.walk('~/uploadedfiles/'):
    for file in files:
        filepath = os.path.join(subdir, file)
        print(filepath)

However, for some reason, I can seem to iterate over the files. The cell just ends with no errors. How do I fix this?


Solution

  • Try replacing the ~ with the full path. Python may not do the bash expansion on that tilde:

    $ cat bork.py 
    #!/usr/bin/env python3
    import os
    
    for subdir, dirs, files in os.walk('/Users/inger.klekacz/parent/'):
        for file in files:
            filepath = os.path.join(subdir, file)
            print(filepath)
    
    

    This worked with this dir structure:

    - parent/
      - foo.txt
      - child1/
        - bar.txt
      - child2/
        - baz.txt
    

    like so:

    $ ./bork.py 
    /Users/inger.klekacz/parent/foo.txt
    /Users/inger.klekacz/parent/child2/baz.txt
    /Users/inger.klekacz/parent/child1/bar.txt
    

    But didn't work when I used the tilde.