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,
JavaScript
x
5
1
for subdir, dirs, files in os.walk('~/uploadedfiles/'):
2
for file in files:
3
filepath = os.path.join(subdir, file)
4
print(filepath)
5
However, for some reason, I can seem to iterate over the files. The cell just ends with no errors. How do I fix this?
Advertisement
Answer
Try replacing the ~ with the full path. Python may not do the bash expansion on that tilde:
JavaScript
1
10
10
1
$ cat bork.py
2
#!/usr/bin/env python3
3
import os
4
5
for subdir, dirs, files in os.walk('/Users/inger.klekacz/parent/'):
6
for file in files:
7
filepath = os.path.join(subdir, file)
8
print(filepath)
9
10
This worked with this dir structure:
JavaScript
1
7
1
- parent/
2
- foo.txt
3
- child1/
4
- bar.txt
5
- child2/
6
- baz.txt
7
like so:
JavaScript
1
5
1
$ ./bork.py
2
/Users/inger.klekacz/parent/foo.txt
3
/Users/inger.klekacz/parent/child2/baz.txt
4
/Users/inger.klekacz/parent/child1/bar.txt
5
But didn’t work when I used the tilde.