import glob
import os
a1_files = glob.glob('A1*')
a2_files = glob.glob('A2*')
for filename in a1_files:
os.rename(filename, os.path.join('A1', filename))
for filename in a2_files:
os.rename(filename, os.path.join('A2', filename))
from os import getcwd
from os import listdir
from os import makedirs
from os.path import join
from os.path import exists
from os.path import abspath
from shutil import move
current_path = getcwd()
for file in listdir("."):
if file.startswith("A"):
full_path = abspath(file)
folder_prefix = file[:2]
folder_path = join(current_path, folder_prefix)
if not exists(folder_path):
makedirs(folder_path)
move(full_path, folder_path)
import os
base_path = "/path/to/files/"
filenames = os.listdir(base_path)
for f in filenames:
source = base_path + f
destination = base_path + f[:2] + "/" + f
os.rename(source, destination)
使用
glob
和os
(shutil
也可以使用):下面是一个经过调整的脚本,我不久前使用它来实现类似的功能:
它复制当前目录中以
A
开头的所有文件,并将它们移动到各自的文件夹中。如果文件夹不存在,它也会预先生成文件夹。您可以通过包含自己的路径来根据自己的喜好进行调整,但它显示了总体思路。你知道吗相关问题 更多 >
编程相关推荐