Tôi đã viết một kịch bản để di chuyển các tệp video từ một thư mục này sang một thư mục khác, nó cũng sẽ tìm kiếm các thư mục con bằng cách sử dụng os.walk. tuy nhiên, nếu tập lệnh tìm thấy tệp video, tập lệnh sẽ chỉ di chuyển tệp và không di chuyển tệp chứa. tôi đã thêm câu lệnh if để kiểm tra xem thư mục chứa có khác với thư mục tìm kiếm gốc hay không.Python: Sử dụng shutil.move hoặc os.rename để di chuyển các thư mục
tôi không thể tìm thấy mã để thực sự di chuyển (hoặc đổi tên?) Thư mục và tệp vào một thư mục khác. tôi đã đọc/xem rất nhiều về di chuyển các tập tin và có rất nhiều thông tin về điều đó, nhưng tôi không thể tìm thấy bất cứ điều gì để di chuyển thư mục.
tôi đã thử sử dụng shutil.move và os.rename và tôi gặp lỗi cả hai lần. khi tôi thử và tìm kiếm sự cố, tôi nhận được rất nhiều kết quả về cách di chuyển tệp hoặc cách thay đổi thư mục làm việc hiện tại của python.
bất kỳ lời khuyên nào (kể cả cách tìm kiếm cụm từ tìm kiếm google để exactatly mô tả cách tìm hướng dẫn về chủ đề) sẽ thực sự được đánh giá cao. đó là chương trình python thế giới thực đầu tiên của tôi và tôi đã học được rất nhiều nhưng bước cuối cùng này là mặc tôi xuống!
EDIT: khi cố gắng sử dụng os.rename(src_file, dst_file)
tôi nhận được lỗi WindowsError: error 3 The system cannot find the path specified.
khi cố gắng shutil.move(src_file, dst_file)
tôi nhận được ioerror errno 2 no such file or directory "H:\\Moviesfrom download...\OneOfTheVideoFilesNotInParentFolder
tức là thư mục và tập tin cần di chuyển.
cảm ơn.
ps như tôi đã nói đó là tập lệnh đầu tiên của tôi bên ngoài học viện mã nên mọi đề xuất ngẫu nhiên cũng sẽ được đánh giá cao.
import os
import shutil
import time
movietypes = ('.3gp', '.wmv', '.asf', '.avi', '.flv', '.mov', '.mp4', '.ogm', '.mkv',
'. mpg', '.mpg', '.nsc', '.nsv', '.nut', '.a52', '.tta', '.wav', '.ram', '.asf',
'.wmv', '. ogg', '.mka', '.vid', '.lac', '.aac', '.dts', '.tac',
'.dts', '.mbv')
filewrite = open('H:\\Movies from download folder\\Logs\\logstest.txt', 'w')
dir_src = "C:\\Users\\Jeremy\\Downloads\\"
dir_dst = "H:\\Movies from download folder\\"
for root, dirs, files in os.walk(dir_src):
for file in files:
if file.endswith(movietypes) == True:
filestr = str(file)
locationoffoundfile = os.path.realpath(os.path.join(root,filestr))
folderitwasin = locationoffoundfile.replace(dir_src,'')
folderitwasin = folderitwasin.replace(filestr,'')
pathofdir = os.path.realpath(root) + "\\"
if pathofdir != dir_src:
src_file = locationoffoundfile
dst_file = dir_dst + folderitwasin + filestr
os.rename(src_file, dst_file) #****This line is the line im having issues with***
print src_file
print dst_file
filewrite.write(file + " " + "needs to have dir and file moved Moved!" + '\n')
else:
src_file = os.path.join(dir_src, file)
dst_file = os.path.join(dir_dst, file)
print src_file
print dst_file
shutil.move(src_file, dst_file)
filewrite.write(os.path.dirname(file) + '\n')
filewrite.write(file + " " + "needs to have file moved Moved!" + '\n')
filewrite.close()
_ "tôi đã thử sử dụng shutil.move và os.rename và tôi gặp lỗi cả hai lần." _ Lỗi nói gì? – Kevin
tôi đã cập nhật bài đăng gốc của mình với các chi tiết về lỗi. – user3560858