根据文件和文件夹名称移动 python 中的文件
Moving files in python based on file and folder name
python 相对较新(不是每天都用)。但是我试图简化一些事情。我基本上有长名称的键,但是键的一个子集(或文件名)具有与相关文件夹相同的序列。{请原谅缩进,它是正确缩进的。} I.E
file1 为:101010-CDFGH-8271.dat 文件夹为 CDFGH-82
file2 为:101010-QWERT-7425.dat 文件夹为 QWERT-74
import os
import glob
import shutil
files = os.listdir("files/location")
dest_1 = os.listdir("dest/location")
for f in files:
file = f[10:21]
for d in dest_1:
dire = d
if file == dire:
shutil.move(file, dest_1)
代码运行没有错误,但是没有任何动作。期待您的回复和学习的机会。
抱歉更新了格式。
尝试变体:
basedir = "dest/location"
for fname in os.listdir("files/location"):
dirname = os.path.join(basedir, fname[10:21])
if os.path.isdir(dirname):
path = os.path.join("files/location", fname)
shutil.move(path, dirname)
python 相对较新(不是每天都用)。但是我试图简化一些事情。我基本上有长名称的键,但是键的一个子集(或文件名)具有与相关文件夹相同的序列。{请原谅缩进,它是正确缩进的。} I.E
file1 为:101010-CDFGH-8271.dat 文件夹为 CDFGH-82 file2 为:101010-QWERT-7425.dat 文件夹为 QWERT-74
import os
import glob
import shutil
files = os.listdir("files/location")
dest_1 = os.listdir("dest/location")
for f in files:
file = f[10:21]
for d in dest_1:
dire = d
if file == dire:
shutil.move(file, dest_1)
代码运行没有错误,但是没有任何动作。期待您的回复和学习的机会。
抱歉更新了格式。
尝试变体:
basedir = "dest/location"
for fname in os.listdir("files/location"):
dirname = os.path.join(basedir, fname[10:21])
if os.path.isdir(dirname):
path = os.path.join("files/location", fname)
shutil.move(path, dirname)