我做錯了什么,我需要改進什么?
import os
source = "C:\\Users\\User\\Desktop\\ala.txt"
destination = "C:\\Users\\User\\Desktop\\zzz.txt"
try:
if os.path.exists(destination):
print("There is already a file there")
else:
os.replace(source,destination)
print(source " was moved")
except FileNotFoundError:
print(source " was not found")
uj5u.com熱心網友回復:
你能試試這個嗎?
import os
os.rename("C:\\Users\\User\\Desktop\\ala.txt", "C:\\Users\\User\\Desktop\\zzz.txt")
uj5u.com熱心網友回復:
您可以嘗試匯入shutil 并呼叫shutil.move(source,destination)。Shutil 模塊提供了移動檔案以及整個檔案夾的功能。
uj5u.com熱心網友回復:
您也可以嘗試使用shutil包,如下所示:
import os
import shutil
source = r"C:\Users\omkar\Desktop\source"
destination = r"C:\Users\omkar\Desktop\destination"
allfiles = os.listdir(source)
for f in allfiles:
shutil.move(source r"\\" f, destination)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325087.html
