简体   繁体   English

Python在Mac上尝试打印文件列表目录时没有此类文件或目录错误

[英]Python no such file or directory error trying to print a file list directory on Mac

I am using Udacity trying to learn to code. 我正在使用Udacity尝试学习编码。 We are on a lesson that is showing us to locate and rename files. 我们正在上一课,向我们展示如何查找和重命名文件。 I am currently trying to get the window to print the list of files in the folder by following the file path using this code: 我目前正在尝试使用以下代码通过遵循文件路径来获取窗口以打印文件夹中的文件列表:

import os

def rename_files():
    # (1) get file names from a folder
    list_files = os.listdir("Macintosh HD⁩ ▸ ⁨Users⁩ ▸ ⁨minstrelking⁩ ▸ ⁨Downloads⁩ ▸ ⁨prank⁩⁩")
    print (list_files)

rename_files()

I have tried many variations of this such as removing "Macintosh HD" or altering it to "Macintosh_HD". 我已经尝试了许多变体,例如删除“ Macintosh HD”或将其更改为“ Macintosh_HD”。 I tried removing folders from the end of the path one by one, and double-checked my spelling constantly. 我尝试一次从路径末尾删除文件夹,然后不断检查我的拼写。 I tried replacing the small arrows ("▸") with forward slashes, then back slashes, and even a recommended combination of the two. 我尝试将小箭头(“▸”)替换为正斜杠,然后替换为反斜杠,甚至推荐两者结合使用。 I tried adding: 我尝试添加:

os.path.expanduser 

As recommended on other forums concerning this issue. 如在其他论坛上有关此问题的建议。 However, I am getting this error: 但是,我收到此错误:

Traceback (most recent call last):
File "/Users/minstrelking/Documents/rename_file.py", line 7, in <module>
rename_files()
File "/Users/minstrelking/Documents/rename_file.py", line 4, in rename_files
print os.listdir("Macintosh HD⁩ ▸ ⁨Users⁩ ▸ ⁨minstrelking⁩ ▸ ⁨Downloads⁩ ▸ ⁨prank⁩⁩")
OSError: [Errno 2] No such file or directory: 'Macintosh HD\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8Users\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8minstrelking\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8Downloads\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8prank\xe2\x81\xa9\xe2\x81\xa9'

I copied and pasted that file path directly from the "GET INFO" box that pops up when you right click the file. 我直接从右键单击文件时弹出的“获取信息”框中复制并粘贴了该文件路径。 That folder "prank" has 30 image files, whose file names should be printing to the terminal. 该文件夹“恶作剧”有30个图像文件,其文件名应打印到终端上。 Does anyone know what I am doing wrong? 有人知道我在做什么错吗?

As you can see in the error trace, this is the actual directory Python is trying to find: 如您在错误跟踪中所见,这是Python试图找到的实际目录:

'Macintosh HD\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8Users\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8minstrelking\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8Downloads\xe2\x81\xa9 \xe2\x96\xb8 \xe2\x81\xa8prank\xe2\x81\xa9\xe2\x81\xa9'

which obviously is not a valid file directory. 这显然不是有效的文件目录。

Instead of copy+pasting the file path directly from the "GET INFO" box, use Terminal to go into the directory you want to search. 而不是直接从“获取信息”框中复制并粘贴文件路径,而是使用终端进入要搜索的目录。 Then type pwd . 然后输入pwd Use what that command prints out. 使用该命令打印出的内容。

You claim you tried using slashes, but I'm almost certain you mistyped something somewhere, so the safest option will be using the output of pwd . 您声称您尝试过使用斜杠,但是我几乎可以肯定您在某处输入了错误的内容,因此最安全的选择是使用pwd的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM