【问题标题】:Python - Automate movement of files to folders that corresponds with its respective numberPython - 自动将文件移动到与其各自编号相对应的文件夹
【发布时间】:2022-12-12 14:05:31
【问题描述】:

我有一个脚本,它接受 cmd 参数并将其移动到具有相应编号的文件夹中。

例如,我有20个名为Episode 1 through Episode 20的文件夹,我总共有120个文件,每个文件夹5个文件(file1-ep 1, file2-ep1 would move into folder "episode 1", all the way to file1-ep20, file2-ep20 would move into folder "episode 20", etc)

我已经有了移动文件的脚本,它只需要我输入命令行参数move.py 1move.py 2,等等。

基本上,我希望程序自动循环,而不是要求用户输入(我只需要提供开始和停止编号)如何进行循环,以便在没有任何用户输入的情况下将所有相应文件移动到文件夹中?

我现在的脚本

import os
import sys
import shutil

path = "/Users/Macbook/Final Cut Pro/"

destination_path = f"/Users/Macbook/Final Cut Pro/episode {sys.argv[1]}/"

for (root, dirs, file) in os.walk(path):
    for f in file:
        f = f.lower()
        if check_file(sys.argv[1])in f:
            source = path + f
            destination = destination_path + f
            print(f)
            # shutil.move(source, destination)

我试过将嵌套的 for 循环放入另一个 for 循环中。

for i in range (1,21)

    for loop
    for loop

【问题讨论】:

    标签: python python-3.x file


    【解决方案1】:

    你能试试这个吗?

    start_number = 1
    end_number = 20
    for i in range(start_number, end_number+1):
        for (root, dirs, file) in os.walk(path):
            for f in file:
                f = f.lower()
                if i in f:
                    source = path + f
                    destination = destination_path + f
                    print(f)
                    shutil.move(source, destination)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多