简体   繁体   English

如何在多个子目录中使用python向文件添加扩展名

[英]How do I add extensions to files using python in multiple subdirectories

I need to add a .jpg extension to around 300K pictures. 我需要为30万张图片添加.jpg扩展名。 They are all in 12 sub-directories and four more subdirectories in each of those 12. 它们全部位于12个子目录中,并且在这12个子目录中又有四个子目录。

I tried following this post but didn't do a walk down to all subdirectories: Adding extension to multiple files (Python3.5) 我尝试了这篇文章,但没有深入所有子目录: 向多个文件添加扩展名(Python3.5)

I also tried the following: 我还尝试了以下方法:

import os

path = 'C:\\Photos'
genmod = os.walk(path)

for path, pathnames, files in gen_obj:
    for file in files:

        head, tail = os.splitext(file)
        if not tail:
            src = os.path.join(path, pathnames, file)
            dst = os.path.join(path, pathnames, file + '.jpg')

            if not os.path.exists(dst): # check if the file doesn't exist
                os.rename(src, dst)

The above runs but nothing happens. 以上运行,但没有任何反应。

The above runs but nothing happens. 以上运行,但没有任何反应。

I doubt that, there are 2 problems: 我怀疑有两个问题:

  • os.splitext should be os.path.splitext os.splitext应该是os.path.splitext
  • os.path.join should not be given pathnames , so os.path.join不应该被赋予pathnames ,所以

     os.path.join(path, pathnames, file) 

    should be 应该

     os.path.join(path, file) 

    and

     os.path.join(path, pathnames, file + '.jpg') 

    should be 应该

     os.path.join(path, file + '.jpg') 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何识别 Python 中的目录和子目录中的 all.txt 文件? - How do I identify all .txt files in a directory and subdirectories in Python? 如何使用递归列出目录和子目录中的现有文件? - how do i list existing files in a directory and subdirectories using recursion? 使用python lxlm解析多个子目录下的多个xml文件 - Using python lxlm to Parse multiple xml files in multiple subdirectories 如何运行 cloc 来检查一个目录(包括子目录)的所有 python 文件,但忽略任何带有迁移一词的子目录? - How do I run cloc to check all python files of a directory including subdirectories but ignore any subdirectories that have the word migrations? 如何在多个子目录中找到具有相同扩展名的所有文件并使用 python 将它们移动到单独的文件夹中? - How can find all files of the same extension within multiple subdirectories and move them to a seperate folder using python? 我有一个 python 脚本来列出路径中的文件,如何过滤具有特定扩展名的文件 - I have a python script to list files in a path, how do I filter files with specific extensions 如何使用Python将文本添加到多个文件名? - How do I add text to multiple filenames using Python? Python 将所有文件从多个子目录移动到不同的对应子目录 - Python move all files from multiple subdirectories to different corresponding subdirectories 如何使用Python获取包含特定文件(即'__main__.py)的子目录列表 - How do i get list of subdirectories contains specific file (ie. '__main__.py) using Python 在所有子目录中运行多个python文件 - Run multiple python files in all subdirectories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM