Python Can';t在google cloud笔记本实例中迭代文件夹中的文件
我正在谷歌云控制台的AI平台中使用笔记本实例。我上传了一个文件夹,里面包含大约30个csv文件 我运行以下代码对文件进行迭代Python Can';t在google cloud笔记本实例中迭代文件夹中的文件,python,google-cloud-platform,jupyter-lab,google-cloud-ai,Python,Google Cloud Platform,Jupyter Lab,Google Cloud Ai,我正在谷歌云控制台的AI平台中使用笔记本实例。我上传了一个文件夹,里面包含大约30个csv文件 我运行以下代码对文件进行迭代 for subdir, dirs, files in os.walk('~/uploadedfiles/'): for file in files: filepath = os.path.join(subdir, file) print(filepath) 然而,出于某种原因,我似乎可以遍历这些文件。单元格的结尾没有错误。如何修复
for subdir, dirs, files in os.walk('~/uploadedfiles/'):
for file in files:
filepath = os.path.join(subdir, file)
print(filepath)
然而,出于某种原因,我似乎可以遍历这些文件。单元格的结尾没有错误。如何修复此问题?尝试将~替换为完整路径。Python可能不会在该tilde上进行bash扩展:
$ 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)
这适用于此目录结构:
- parent/
- foo.txt
- child1/
- bar.txt
- child2/
- baz.txt
像这样:
$ ./bork.py
/Users/inger.klekacz/parent/foo.txt
/Users/inger.klekacz/parent/child2/baz.txt
/Users/inger.klekacz/parent/child1/bar.txt
但我用瓷砖的时候没用