[英]Move first 5 files out of 100 from one directory to another using Python code
是否可以使用 python 代碼將前 5 個文件從一個目錄移動到另一個目錄? 我必須在數據磚筆記本上運行代碼。
場景是:我必須選擇目錄中存在的前 5 個文件(總文件為 100)並將這 5 個文件移動到另一個目錄,這個過程將重復直到所有文件移動到另一個文件夾。
r'' - 原始字符串文字(忽略字符串中的反斜杠)
import os
import shutil
source = r'C:\Python38-32' # files location
destination = r'C:\New Folder' # where to move to
folder = os.listdir(source) # returns a list with all the files in source
while folder: # True if there are any files, False if empty list
for i in range(5): # 5 files at a time
file = folder[0] # select the first file's name
curr_file = source + '\\' + file # creates a string - full path to the file
shutil.move(curr_file, destination) # move the files
folder.pop(0) # Remove the moved file from the list
為我工作,將文件從一個目錄( source
)移動到另一個( destination
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.