Python “移动指定数量的特定文件”

huangapple go评论66阅读模式
英文:

Python "move a specefied number of certain files"

问题

I am trying to move a specific number of certain files.

for file_names in file_names[:12]:
    if os.path.isfile(file_names):
        if file_names.endswith('.txt'):
            shutil.move(os.path.join(dir_path, file_names), nf)

The original directory could have 0 to 70 something files no sub folders. It is supposed to run through and create a new folder. Then move 12 txt files into that folder, then repeat. The issue comes from the fact. That the array number counts 12 items, including the folders and moves only the txt items in that array. Sorry hopefully using the right terminology.

So, what happens is it creates the first folder and moves 11 txt files. Then the next folder and moves 9 text files and so on.

So how do get it move 12 text files consistently even with new folders being added to the directory?

英文:

I am trying to move a specific number of certain files.

for file_names in file_names[:12]:
    if os.path.isfile(file_names):
        if file_names.endswith('.txt'):
            shutil.move(os.path.join(dir_path, file_names), nf)

The original directory could have 0 to 70 something files no sub folders. It is supposed to run through and create a new folder. Then move 12 txt files into that folder, then repeat. The issue comes from the fact. That the array number counts 12 items, including the folders and moves only the txt items in that array. Sorry hopefully using the right terminology.

So, what happens is it creates the first folder and moves 11 txt files. Then the next folder and moves 9 text files and so on.

So how do get it move 12 text files consistently even with new folders being added to the directory?

答案1

得分: 1

有很多方法可以解决这个问题。也许可以使用 glob 递归搜索以 '.txt' 结尾的文件。

使用基本函数,先尝试筛选文件列表吧。

file_names_filtered = [x for x in file_names if os.path.isfile(x) and x.endswith('.txt')]

for file_names in file_names_filtered[:12]:
    shutil.move(os.path.join(dir_path, file_names), nf)
英文:

There are many ways you could get around this. Maybe use glob to search the directory recursively for files ending in '.txt'.

Using basic functions, how about filtering the list of files first?

file_names_filtered = [x for x in file_names if os.path.isfile(x) and x.endswith('.txt')]

for file_names in file_names_filtered[:12]:
        shutil.move(os.path.join(dir_path, file_names), nf)

答案2

得分: 0

问题在于你在检查它们是否为文件而不是目录之前,对文件夹中的路径进行了子集化(即for file_names in file_names[:12]:)。你可能需要重新思考脚本的逻辑。

有许多方法可以做到这一点。其中一种方法是使用计数器并在源目录中维护一个txt文件列表。

类似这样的代码(未经测试):

# 设置源目录
source_dir = 'path/to/source/dir'

# 获取源目录中所有文本文件的列表
txt_files = [f for f in os.listdir(source_dir) if f.endswith('.txt')]

# 为新文件夹名称设置计数器
new_folder_count = 1

# 遍历文本文件列表,每次移动12个,直到没有剩余文件
while len(txt_files) > 0:
    # 为当前文件批次创建目标目录
    dest_dir = os.path.join(source_dir, f'nf_{new_folder_count}')
    os.mkdir(dest_dir)

    # 将前12个文本文件移动到目标目录
    for file_name in txt_files[:12]:
        file_path = os.path.join(source_dir, file_name)
        shutil.move(file_path, dest_dir)

    # 从文本文件列表中删除已移动的文件
    txt_files = txt_files[12:]

    # 增加新文件夹计数器
    new_folder_count += 1
英文:

The issue is that you are subsetting paths in the folder (i.e. for file_names in file_names[:12]:) before you check if each of them are files and not directories (i.e. if os.path.isfile(file_names):). You may need to rethink the logic of your script.

There are many ways to do this. One way is use a counter and maintain a list of txt files in your source directory.

Something like this (code note tested):

# Set the source directory
source_dir = 'path/to/source/dir'

# Get a list of all the text files in the source directory
txt_files = [f for f in os.listdir(source_dir) if f.endswith('.txt')]

# Set a counter for the new folder names
new_folder_count = 1

# Iterate through the list of text files, moving 12 at a time until none are left
while len(txt_files) > 0:
    # Create the destination directory for the current batch of files
    dest_dir = os.path.join(source_dir, f'nf_{new_folder_count}')
    os.mkdir(dest_dir)

    # Move the first 12 text files to the destination directory
    for file_name in txt_files[:12]:
        file_path = os.path.join(source_dir, file_name)
        shutil.move(file_path, dest_dir)

    # Remove the moved files from the list of text files
    txt_files = txt_files[12:]

    # Increment the new folder counter
    new_folder_count += 1

huangapple
  • 本文由 发表于 2023年4月13日 22:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定