簡體   English   中英

重命名Python 2.7中的文件擴展名

[英]Renaming file extension in Python 2.7

我試圖按照此處的建議將文本文件的擴展名重命名為zip。 文件是根據服務器的base64編碼響應寫入的,我在寫入之前先對其進行解碼。

這是我的代碼段:

f = open("response.txt","wb")
f.write(json.loads(response.text)['Binary'].decode('base64'))
f.close()
file1 = "C:\Users\xyz\response.txt"
base = os.path.splitext(file1)[0]
os.rename(file1, base + ".zip")

即使文件位於我的代碼中指定的絕對路徑中,我仍收到以下錯誤:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect

請協助。

file1 = "C:\Users\xyz\response.txt"

“ \\ r”是代表回車符的單個字符。 您可能沒有名稱中帶有回車符的文件。 如果您希望將其作為反斜杠后跟R,請使用原始字符串。

file1 = r"C:\Users\xyz\response.txt"

嘗試更改此行:

file1 = "C:\Users\xyz\response.txt"

對此:

file1 = "C:\\Users\\xyz\\response.txt"

或這個:

file1 = r"C:\Users\xyz\response.txt"

暫無
暫無

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

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