即使jupyter笔记本中的shell命令显示目录
我正在尝试对 OneDrive 文件夹中的图像目录运行函数。当我在 jupyter 笔记本中使用 shell 命令时:
!ls ../OneDrive\ -\ University/centralschool
我得到了所有 .tif 图像的列表。但是,当我尝试通过 python 函数调用该目录时:
for f in sorted(listdir('../OneDrive\ -\ University/centralschool')):
print(f)
我收到经典错误:
FileNotFoundError: [Errno 2] No such file or directory: '../OneDrive\\ -\\ University/centralschool'
我以前从未从 OneDrive 文件夹中调用过文件夹,这是不可能的,因为它是基于云的?另外,你调用一个包含空格的文件夹的方式也让我绊倒了,但是一旦我让它在 shell 命令中工作,我以为它会转移到我的 python 函数中,但事实并非如此。
最后,我正在使用的脚本设置如下,
Desktop
Documents
OneDrive
Image_Work
|
|__script.ipynb
|__otherscripty.ipynb
我使用的脚本定义为“脚本”,因此我尝试使用 ./
和 启动目录调用。 ./
.我通过 Anaconda 使用 jupyter 笔记本。
I am attempting to run a function on a directory of images in my OneDrive folder. When I use the shell command in the jupyter notebook:
!ls ../OneDrive\ -\ University/centralschool
I get a list of all my .tif images. However, when I attempt to call the directory through my python function:
for f in sorted(listdir('../OneDrive\ -\ University/centralschool')):
print(f)
I get the classic error:
FileNotFoundError: [Errno 2] No such file or directory: '../OneDrive\\ -\\ University/centralschool'
I've never called folders from my OneDrive folder before, is this not possible since it is cloud-based? Also the way that you call a folder that has spaces in it tripped me up but once I got it to work in the shell command I thought it would transfer into my python function but that wasn't the case.
Lastly, the script I am working in is set up as follows
Desktop
Documents
OneDrive
Image_Work
|
|__script.ipynb
|__otherscripty.ipynb
With the script I'm using defined as "script", thus i've tried starting my directory call with ./
and ../
. I am using jupyter notebook through Anaconda.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为文件名位于 python 中的字符串中,所以无需担心转义空格。 (如果将文件名放入引号中,则也不需要在命令行上执行此操作。)该问题与 OneDrive 无关。
这应该有效:
Because you have the file name inside a string in python, you don't need to worry about escaping the spaces. (You also wouldn't need to do so on the command line if you put the file name into quotes.) The issue has nothing to do with OneDrive.
This should work: