组织文件并将其复制到新文件夹
Organizing and copying files to new folders
我正在尝试在处理之前整理一些数据。
我有一个包含原始 tiff 文件的文件夹(它们是来自无人机传感器的光栅波段)。
我想将这些文件移动到新的、单独的文件夹中。例如,IMG_001_1、IMG_001_2、IMG_001_3、IMG_001_4 和 IMG_001_5 都移动到名为 IMG_001 的新文件夹中。我可以更改文件的命名结构以使代码更简单。
另一个问题是文件夹中缺少一些图像。当前文件为 IMG0016 - IMG0054(无 IMG0055)、IMG0056 - IMG0086(无 IMG0087)和 IMG0087 - IMG0161。这就是为什么我认为将新图像文件夹从 1-143 重命名会更简单。
我的主要问题实际上是将文件移动到新文件夹中 - 创建文件夹非常简单。
玩了一会儿,这个脚本出来了,它应该做你想要的:
import os
import shutil
import re
UNORG = "C:\Users\joshuarb\Desktop\Unorganized_Images\"
ORG = "C:\Users\joshuarb\Desktop\Organized_Images\"
def main():
file_names = [os.path.join(UNORG, i) for i in get_files_of(UNORG)]
for count in range(0, 143):
current_dir = "{}IMG_{:04d}".format(ORG, count)
os.makedirs(current_dir)
move_files = get_files_to_move(file_names, count)
print move_files
for i in move_files:
shutil.move(i, os.path.join(current_dir, os.path.basename(i)))
def get_files_to_move(file_names, count):
return [i for i in file_names if re.match('.*IMG{}_.*'.format(count), i)]
def get_files_of(mypath):
(dirpath, dirnames, filenames) = os.walk(mypath).next()
return filenames
if __name__ == '__main__':
main()
如您所见,代码没有注释。但如果有不清楚的地方,请随时询问;)
问题已解决!
import os
import shutil
srcpath = "C:\Users\joshuarb\Desktop\Python_Test\UnorganizedImages"
srcfiles = os.listdir(srcpath)
destpath = "C:\Users\joshuarb\Desktop\Python_Test\OrganizedImages"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:8] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for dirname, full_path in targets:
for filename in srcfiles:
if dirname == filename[0:8]:
move(filename, full_path)
我正在尝试在处理之前整理一些数据。
我有一个包含原始 tiff 文件的文件夹(它们是来自无人机传感器的光栅波段)。
我想将这些文件移动到新的、单独的文件夹中。例如,IMG_001_1、IMG_001_2、IMG_001_3、IMG_001_4 和 IMG_001_5 都移动到名为 IMG_001 的新文件夹中。我可以更改文件的命名结构以使代码更简单。
另一个问题是文件夹中缺少一些图像。当前文件为 IMG0016 - IMG0054(无 IMG0055)、IMG0056 - IMG0086(无 IMG0087)和 IMG0087 - IMG0161。这就是为什么我认为将新图像文件夹从 1-143 重命名会更简单。
我的主要问题实际上是将文件移动到新文件夹中 - 创建文件夹非常简单。
玩了一会儿,这个脚本出来了,它应该做你想要的:
import os
import shutil
import re
UNORG = "C:\Users\joshuarb\Desktop\Unorganized_Images\"
ORG = "C:\Users\joshuarb\Desktop\Organized_Images\"
def main():
file_names = [os.path.join(UNORG, i) for i in get_files_of(UNORG)]
for count in range(0, 143):
current_dir = "{}IMG_{:04d}".format(ORG, count)
os.makedirs(current_dir)
move_files = get_files_to_move(file_names, count)
print move_files
for i in move_files:
shutil.move(i, os.path.join(current_dir, os.path.basename(i)))
def get_files_to_move(file_names, count):
return [i for i in file_names if re.match('.*IMG{}_.*'.format(count), i)]
def get_files_of(mypath):
(dirpath, dirnames, filenames) = os.walk(mypath).next()
return filenames
if __name__ == '__main__':
main()
如您所见,代码没有注释。但如果有不清楚的地方,请随时询问;)
问题已解决!
import os
import shutil
srcpath = "C:\Users\joshuarb\Desktop\Python_Test\UnorganizedImages"
srcfiles = os.listdir(srcpath)
destpath = "C:\Users\joshuarb\Desktop\Python_Test\OrganizedImages"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:8] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for dirname, full_path in targets:
for filename in srcfiles:
if dirname == filename[0:8]:
move(filename, full_path)