Python:使用名称较短的重新创建文件路径?
D:\ mydir包含有或没有子目录的大量目录,这些目录可能包含一个或多个文件。
他们中的一些子目录和/或文件也有很长的名称+/-非常长的名称,这也导致该文件的巨大路径名。
例如,
D:\MyDir:
"Very large directory name"\
"Very large directory name"\
#OPTIONAL
#other or the same name repeating one or more times
"normal dir"\ #optional
Long_or_normal_name_MyFile.html and Long_or_normal_name_MyFile.html and so on...
我想创建一个python脚本,该脚本:
- 将文件移至名为“非常大目录名称”的第一个目录(基本目录)。
- 用较短的名称重命名文件。
- 用较短的名称重命名基本目录,然后删除留下的空目录。
要移动文件或文件夹,我可以使用shutil.move()来完成此操作。
使用此代码,我列出了目录中的所有文件并将它们存储到列表中:
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
此行将仅列出d:\ mydir的第一级目录,然后将它们存储到列表中:
[ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
但是如何进一步执行以做什么我需要做吗?
忘了说D:\ mydir还包含不需要重命名和/或移动的目录和文件。
先感谢您!
D:\MyDir contains a huge number of directories with or without subdirectories which may or may not contain one or more files.
Some of them have very long name +/- very long name for the subdirectories and/or file(s) too which leads to a huge path name for that file.
e.g.
D:\MyDir:
"Very large directory name"\
"Very large directory name"\
#OPTIONAL
#other or the same name repeating one or more times
"normal dir"\ #optional
Long_or_normal_name_MyFile.html and Long_or_normal_name_MyFile.html and so on...
I want to create a Python script that:
- Move the file(s) to the first directory (base directory) named "Very large directory name".
- Rename the file with a shorter name.
- Rename the base directory with a shorter name and after that remove empty directories left behind.
To move a file or a folder I can do that with shutil.move().
With this code I list all the files from a directory and store them into a list:
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
And this line will list only first level directory from D:\MyDir and store them into a list:
[ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
But how to do it further in order to do what I need to do?
Forgot to say that D:\MyDir contains also directories and files that don't need to be renamed and/or moved.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以穿越要更改为底层的每个文件夹。将每个文件移到下至两个级别上,然后更改其名称。然后卸下返回的所有空文件夹。然后最终更改目录本身的名称。
这是一个例子。不知道您对每个文件夹重命名的计划
文件我刚刚取现在名称并将其切成两半。我确实保留文件扩展名,并检查以确保更名的文件不会通过在末端添加唯一数字来相互覆盖。
You could traverse each folder that you wanted to change to the bottom level. Move each file at the bottom up two levels and change their names. Then remove any empty folders on your way back up. Then finally change the name of the directory itself.
Here is an example. Without knowing what your planning on renaming each folder
and file I just took their current names and cut them in half. I do preserve file extensions and check to make sure the renamed files aren't overwriting each other by adding unique numbers to the end.