即使jupyter笔记本中的shell命令显示目录

发布于 2025-01-19 16:46:26 字数 806 浏览 1 评论 0原文

我正在尝试对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-01-26 16:46:26

因为文件名位于 python 中的字符串中,所以无需担心转义空格。 (如果将文件名放入引号中,则也不需要在命令行上执行此操作。)该问题与 OneDrive 无关。

这应该有效:

for f in sorted(listdir('../OneDrive - University/centralschool')):
    print(f)

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:

for f in sorted(listdir('../OneDrive - University/centralschool')):
    print(f)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文