簡體   English   中英

Python os.rename 問題: FileNotFoundError: [WinError 3] 系統找不到指定的路徑

[英]Python os.rename issues: FileNotFoundError: [WinError 3] The system cannot find the path specified

在發布之前,我已經瀏覽過這個問題的答案,但找不到適合我的答案。

所以基本上我正在做的是獲取 selenium 元素的列表,將它們轉換為新數組中的文本,然后嘗試在我的下載目錄中重命名文件。
到達這個循環后,我得到了錯誤。
新文件是否必須存在才能重命名?

for count, filename in enumerate(os.listdir("C:/Users/user/Downloads")):
    dst1 = titlelist[count] + ".mp4"
    src = 'C:/Users/user/Downloads/'+ filename
    dst = 'C:/Users/user/Downloads/'+ dst1
    os.rename(src, dst)

任何幫助表示贊賞。

要訪問 Windows 路徑,您需要使用反斜杠,並且由於反斜杠是轉義字符,因此您需要使用其中兩個來將它們包含在字符串中。

而不是寫C:/Users/user你應該寫C:\\Users\\user

我知道這聽起來很傻,但是,您可能想檢查文件夾和文件的權限

print("Source File Permission : %s" % os.access(src ,os.W_OK))
print("Destination File Permission : %s" % os.access(dst , os.W_OK))

在您的情況下,我很想嘗試以下方法:

if os.access('C:/Users/user/Downloads/', os.W_OK):
    os.rename(src, dst)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM