使用Python代码将100个文件中的前5个文件从一个目录移动到另一个目录
是否可以使用python代码将前5个文件从一个目录移动到另一个目录? 我必须在数据块笔记本上运行代码 场景是:我必须选择目录中出现的任何前5个文件(总文件数为100)并将这5个文件移动到另一个目录,此过程将重复,直到所有文件移动到另一个文件夹。r'-rawstring literal(忽略字符串中的反斜杠) 为我工作,将文件从一个目录(使用Python代码将100个文件中的前5个文件从一个目录移动到另一个目录,python,azure-databricks,Python,Azure Databricks,是否可以使用python代码将前5个文件从一个目录移动到另一个目录? 我必须在数据块笔记本上运行代码 场景是:我必须选择目录中出现的任何前5个文件(总文件数为100)并将这5个文件移动到另一个目录,此过程将重复,直到所有文件移动到另一个文件夹。r'-rawstring literal(忽略字符串中的反斜杠) 为我工作,将文件从一个目录(source)移动到另一个目录(destination)r'-rawstring literal(忽略字符串中的反斜杠) 为我工作,将文件从一个目录(source
source
)移动到另一个目录(destination
)r'-rawstring literal(忽略字符串中的反斜杠)
为我工作,将文件从一个目录(
source
)移动到另一个目录(destination
)你说的“前5个”是什么意思?选择目录中的前5个文件(总文件数为100),然后将这5个文件移动到另一个目录,此过程将重复,直到所有文件移动到另一个文件夹。您使用的是什么操作系统?它是运行在Linux内核上的azure databricks笔记本。我通过Linux命令“%sh find.-maxdepth 1-type f|head-2|xargs mv-t”/dbfs/mnt/dev/Devices/temp/xml/”实现了这个结果,但数据块上的问题是,我不能在一个单元格中运行Linux命令和python命令。你说的“前5个”是什么意思?选择目录中出现的任何前5个文件(总文件数为100)并将这5个文件移动到另一个目录,此过程将重复,直到所有文件移动到另一个文件夹。您使用的是什么操作系统?它是运行在Linux内核上的azure databricks笔记本。我通过Linux命令“%sh find.-maxdepth 1-type f | head-2 | xargs mv-t”/dbfs/mnt/dev/Devices/temp/xml/”实现了这个结果,但数据块上的问题是,我无法在一个单元格中运行Linux命令和python命令。
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