【问题标题】:How to give windows directory path in os.listdir(path)?如何在 os.listdir(path) 中给出 windows 目录路径?
【发布时间】:2023-04-09 15:04:01
【问题描述】:

当我试图在 os.listdir() 中给出 windows 目录路径时,它给出了错误。 我的代码sn-p:

with os.listdir('C:\Users\Hp\Desktop\video') as entries:

我知道 python 将 '\' 作为转义序列,但我在 Windows 上找不到任何替代方案。给出的错误是:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX

我尝试了以下解决方案,但它给了我一些其他错误:-

AttributeError: __enter__

我的代码有问题吗:

import os
import moviepy.editor as mp

#location = os.path.join("C:", "Users", "Hp", "Desktop", "video")

with os.listdir("C:\\Users\\Hp\\Desktop\\video") as entries:
    for entry in entries:
        if(".py" or ".png") not in entry:
            video = mp.VideoFileClip("entry.name")

            logo = (mp.ImageClip("logo.png")
                      .set_duration(video.duration)
                      .resize(height=50) # if you need to resize...
                      .margin(right=8, top=8, opacity=0) # (optional) logo-border padding
                      .set_pos(("right","top")))

            final = mp.CompositeVideoClip([video, logo])
            final.write_videofile('o' + "entry.name")

【问题讨论】:

  • 添加 `\\` 像这样with os.listdir("C:\\Users\\Hp\\Desktop\\video") as entries:
  • 使用'C:\\Users\\Hp\\Desktop\\video''C:/Users/Hp/Desktop/video'
  • 我尝试了你的两个解决方案,我得到了这个错误:“AttributeError: enter
  • 将 'r' 放在文件路径的前面。像这样 r"C:\\Users\\Hp\\Desktop\\video"。现在 python 将其读取为原始字符串。

标签: python python-3.x windows python-2.7


【解决方案1】:

使用忽略反斜杠作为转义字符的原始字符串

with os.listdir(r'C:\Users\Hp\Desktop\video') as entries:

或者使用文字反斜杠(转义的反斜杠)

with os.listdir('C:\\Users\\Hp\\Desktop\\video') as entries:

或者只使用正斜杠。它们在 Windows 中运行。

with os.listdir('C:/Users/Hp/Desktop/video') as entries:

【讨论】:

  • 它在您上面提供的所有 3 个解决方案中都给了我这个错误:“AttributeError: enter
  • 要作为上下文管理器工作,请使用os.scandir 而不是os.listdir,并迭代entries
  • 我认为任何提及在 Windows 中使用正斜杠作为路径分隔符都需要注意,Windows API 中的许多情况都需要反斜杠,例如“\\?\”扩展路径和 shell @987654321 @ 职能。此外,一些程序将正斜杠解析为其命令行中的选项开关,并要求路径分隔符仅为反斜杠,因此首先规范化通过os.path.normpath 在命令行中传递的路径。
【解决方案2】:

@Adam Smith 的回答是正确的。但我想指出一个错误。 os.listdir 不返回上下文管理器(只是一个普通列表),因此将它与 with 关键字一起使用是没有用的。正常调用函数。

entries = os.listdir('C:\Users\Hp\Desktop\video')

您不会收到AttributeError: __enter__ 错误。

with 关键字是在块之前调用obj.__enter__() 并在其之后调用obj.__exit__() 的自动方式。如果对象(这里是从os.listdir 返回的列表)没有这些方法,你会得到一个错误。

【讨论】:

    【解决方案3】:
    import os
    import moviepy.editor as mp
    
    path="C:\\Users\\Hp\\Desktop\\video"
    
    entries=os.listdir(path)
    for entry in entries:
        if(".py" or ".png") not in entry:
            video = mp.VideoFileClip("entry.name")
    
            logo = (mp.ImageClip("logo.png")
                    .set_duration(video.duration)
                    .resize(height=50) # if you need to resize...
                    .margin(right=8, top=8, opacity=0) # (optional) logo-border padding
                    .set_pos(("right","top")))
    
            final = mp.CompositeVideoClip([video, logo])
            final.write_videofile('o' + "entry.name")
    

    【讨论】:

      【解决方案4】:

      最好使用pathlib python 库。这样的问题在里面处理得很好。

      from pathlib import Path
      p = Path('C:\Users\Hp\Desktop\video') # Use any format
      for file in p.iterdir():
          # Every 'file' is a 'Path' variable with full path
          file.name # Returns full filename
          file.suffix # Returns extension. e.g. '.jpg'
          str(file) # Returns path as python string
      

      请参考here了解更多信息。

      【讨论】:

        猜你喜欢
        • 2012-12-05
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多