Python 根据不同文件夹中的名称重命名文件夹中的文件
我有两个文件夹,每个文件夹都有相同数量的文件。我想根据文件夹1中文件的名称重命名文件夹2中的文件。因此,在文件夹1中,可能有三个文件名为: 陆地卫星1号, 陆地卫星2号, 陆地卫星3号 在文件夹2中,这些文件称为: 一,, 2. 三, 我想根据文件夹1的名称重命名它们。我考虑过将每个文件夹的项目名称转换为.txt文件,然后将.txt文件转换为列表,然后重命名,但我不确定这是否是最好的方法。有什么建议吗 编辑: 我已经简化了上面的文件名,所以仅仅附加Landsat_uuuu对我不起作用Python 根据不同文件夹中的名称重命名文件夹中的文件,python,text-files,rename,Python,Text Files,Rename,我有两个文件夹,每个文件夹都有相同数量的文件。我想根据文件夹1中文件的名称重命名文件夹2中的文件。因此,在文件夹1中,可能有三个文件名为: 陆地卫星1号, 陆地卫星2号, 陆地卫星3号 在文件夹2中,这些文件称为: 一,, 2. 三, 我想根据文件夹1的名称重命名它们。我考虑过将每个文件夹的项目名称转换为.txt文件,然后将.txt文件转换为列表,然后重命名,但我不确定这是否是最好的方法。有什么建议吗 编辑: 我已经简化了上面的文件名,所以仅仅附加Landsat_uuuu对我不起作用 文件夹1中
文件夹1中的真实文件名更像LT503002011_band1、LT5040300201_band1、LT50402312_band4。在文件夹2中,它们是extract1、extract2、extract3。文件夹2中总共有500个文件,这只是提取的一个运行计数和每个文件的一个数字。您可能需要使用glob包,它采用文件名模式并将其输出到列表中。例如,在该目录中
glob.glob('*')
给你
['Landsat_1', 'Landsat_2', 'Landsat_3']
然后,您可以循环列表中的文件名,并相应地更改文件名:
import glob
import os
folderlist = glob.glob('*')
for folder in folderlist:
filelist = glob.glob(folder + '*')
for fil in filelist:
os.rename(fil, folder + fil)
希望这能有所帮助正如有人所说,“对每个列表进行排序并将它们压缩在一起以便重命名”
注:
函数提取所有数字,以便key()
可以根据嵌入的数字对列表进行数字排序sorted()
- 我们对这两个列表进行排序:
以任意顺序返回文件os.listdir()
循环是使用zip的常用方法:for
for itemA,itemB in zip(listA,listB):
提供了可移植性:无需担心os.path.join()
或/
\
- Windows上的典型调用:
,假设这些是您描述的目录python doit.py c:\data\lt c:\data\extract
- 对*nix的典型调用::
python doit.py./lt./extract
我追求更多的完整性:D
# WARNING: BACKUP your data before running this code. I've checked to
# see that it mostly works, but I would want to test this very well
# against my actual data before I trusted it with that data! Especially
# if you're going to be modifying anything in the directories while this
# is running. Also, make sure you understand what this code is expecting
# to find in each directory.
import os
import re
main_dir_demo = 'main_dir_path'
extract_dir_demo = 'extract_dir_path'
def generate_paths(directory, filenames, target_names):
for filename, target_name in zip(filenames, target_names):
yield (os.path.join(directory, filename),
os.path.join(directory, target_name))
def sync_filenames(main_dir, main_regex, other_dir, other_regex, key=None):
main_files = [f for f in os.listdir(main_dir) if main_regex.match(f)]
other_files = [f for f in os.listdir(other_dir) if other_regex.match(f)]
# Do not proceed if there aren't the same number of things in each
# directory; better safe than sorry.
assert len(main_files) == len(other_files)
main_files.sort(key=key)
other_files.sort(key=key)
path_pairs = generate_paths(other_dir, other_files, main_files)
for other_path, target_path in path_pairs:
os.rename(other_path, target_path)
def demo_key(item):
"""Sort by the numbers in a string ONLY; not the letters."""
return [int(y) for y in re.findall('\d+', item)]
def main(main_dir, extract_dir, key=None):
main_regex = re.compile('LT\d+_band\d')
other_regex = re.compile('extract\d+')
sync_filenames(main_dir, main_regex, extract_dir, other_regex, key=key)
if __name__ == '__main__':
main(main_dir_demo, extract_dir_demo, key=demo_key)
请确保您的脚本来自父目录。然后将每个列表和
zip
一起排序以便重命名。您可能应该显示一个更真实的文件名示例,除非您的真实文件名也只是数字的前缀。哦,那么您只想按排序顺序匹配它们?例如,“extract1”改名为“LT503002011\u band1”,“extract2”改名为“LT5040300201\u band1”,等等?是的,这正是我想要做的!glob.glob命令中的星号是指向文件名的路径吗?第二个星号是我要将文件名更改为的吗?星号只是表示应选择哪些文件/文件夹。“星号”表示当前目录中的所有文件,“Landsat_2/星号”表示Landsat_2中的所有文件。(很抱歉,由于某些原因,星号不会出现在评论中)我不熟悉使用python作为访问操作系统的方式,我主要使用它来处理csv文件,所以请原谅可能是一个简单的问题。ltdir
是否等效于我的文件夹1的路径和exdir
是否等效于我的文件夹2的路径?是的,变量ltdir
和exdir
接受命令行上传入的任何值。在这种情况下,我希望您可以输入上面描述的相应目录路径。我将尝试一下,谢谢您的帮助!在运行我的真实数据之前,我肯定会使用数据集的副本,谢谢大家的关注。
# WARNING: BACKUP your data before running this code. I've checked to
# see that it mostly works, but I would want to test this very well
# against my actual data before I trusted it with that data! Especially
# if you're going to be modifying anything in the directories while this
# is running. Also, make sure you understand what this code is expecting
# to find in each directory.
import os
import re
main_dir_demo = 'main_dir_path'
extract_dir_demo = 'extract_dir_path'
def generate_paths(directory, filenames, target_names):
for filename, target_name in zip(filenames, target_names):
yield (os.path.join(directory, filename),
os.path.join(directory, target_name))
def sync_filenames(main_dir, main_regex, other_dir, other_regex, key=None):
main_files = [f for f in os.listdir(main_dir) if main_regex.match(f)]
other_files = [f for f in os.listdir(other_dir) if other_regex.match(f)]
# Do not proceed if there aren't the same number of things in each
# directory; better safe than sorry.
assert len(main_files) == len(other_files)
main_files.sort(key=key)
other_files.sort(key=key)
path_pairs = generate_paths(other_dir, other_files, main_files)
for other_path, target_path in path_pairs:
os.rename(other_path, target_path)
def demo_key(item):
"""Sort by the numbers in a string ONLY; not the letters."""
return [int(y) for y in re.findall('\d+', item)]
def main(main_dir, extract_dir, key=None):
main_regex = re.compile('LT\d+_band\d')
other_regex = re.compile('extract\d+')
sync_filenames(main_dir, main_regex, extract_dir, other_regex, key=key)
if __name__ == '__main__':
main(main_dir_demo, extract_dir_demo, key=demo_key)