python,将所有文件从目录树的第3,第4,第5层移动到第2层

python,将所有文件从目录树的第3,第4,第5层移动到第2层,第1张

概述python,将所有文件目录树的第3,第4,第5层移动到第2层

我知道我们有os.walk但我不知道如何创build这个。

比方说,我有一个Ubuntu的linux机器上的以下文件夹结构:

Maindir (as root called by script) +- subdir-one | +-subdir-two | +-file | +-another file | +-subdir-three | +-file3 | +-file4 | +-subdir-four | +- file5 | +- file6 +- subdir-two +- subdir-three | +-sub-subdir-two | +-file | +-another file | +-subdir-three | +-file3 | +-file4 | +-subdir-four | +-file5 | +-file6 +-subdir-four +-subdir-two +-file +-another file +-subdir-three +-file3 +-file4 +-subdir-four +-file5 +-file6

我想将所有文件从子目录移动到级别2的子级,而不是根级别。

以subdir-one为例:将subdir-4中的所有文件移动到subdir-1(本例中为file5和file6),将所有文件从subdir-3移动到subdir-1(本例中为file3和file4)

gcc编译错误cv.h找不到Opencv

通过鼠标button触发自动键脚本 – 如何?

在RPM CentOS规范文件中使用自定义的configure / make构build多个源文件

从文本表示生成UML图

在linux中用邮件或者mutt替代发送邮件?

Subdir-2没有其他的subdirs,所以可以跳过脚本。

Subdir-three:将所有文件从sub-subdir-2,subdir-3和subdir-4移动到subdir-3。

我认为你说对了。 没有问题,如果文件被覆盖,如果他们有相同的名字,无论如何都是重复的,这是运行这个清理脚本的一个原因。

当所有的文件都从子目录移动,这意味着子目录将是空的,所以我也想删除空的子目录。

14 – 2012年更新:这是从jcollado给出的改变的代码,但仍然无法正常工作。 顺便说一句,我忘了提及,我也需要过滤一些目录名称。 在目录树中find这些目录名称需要被排除

我稍微改变了代码:

import os,sys def main(): try: main_dir = sys.argv[1] print main_dir # Get a List of all subdirectorIEs of main_dir subdirs = filter(os.path.isdir,[os.path.join(main_dir,path) for path in os.Listdir(main_dir)]) print subdirs # For all subdirectorIEs,# collect all files and all subdirectorIEs recursively for subdir in subdirs: files_to_move = [] subdirs_to_remove = [] for dirpath,dirnames,filenames in os.walk(subdir): files_to_move.extend([os.path.join(dirpath,filename) for filename in filenames]) subdirs_to_remove.extend([os.path.join(dirpath,dirname) for dirname in dirnames]) # To move files,just rename them replacing the original directory # with the target directory (subdir in this case) print files_to_move print subdirs_to_remove for filename in files_to_move: source = filename destination = os.path.join(subdir,os.path.basename(filename)) print 'Destination ='+destination if source != destination: os.rename(source,destination) else: print 'Rename cancelled,source and destination were the same' # Reverse subdirectorIEs order to remove them # starting from the lower level in the tree hIErarchy subdirs_to_remove.reverse() # Remove subdirectorIEs for dirname in subdirs_to_remove: #os.rmdir(dirname) print dirname except ValueError: print 'Please supply the path name on the command line' if __name__ == '__main__': main()

在多进程程序中查找哪些函数被调用而不修改源代码?

linux上C的networking摄像头库?

Makefile:检查目录的写入权限

linux中使用扩展字符(128-255)的open()函数返回-1错误

如何检测linux上的chmod文件

我想如下:

import os main_dir = 'main' # Get a List of all subdirectorIEs of main_dir subdirs = filter(os.path.isdir,path) for path in os.Listdir(main_dir)]) # For all subdirectorIEs,just rename them replacing the original directory # with the target directory (subdir in this case) for filename in files_to_move: source = filename destination = os.path.join(subdir,os.path.basename(filename)) os.rename(source,destination) # Reverse subdirectorIEs order to remove them # starting from the lower level in the tree hIErarchy subdirs_to_remove.reverse() # Remove subdirectorIEs for dirname in subdirs_to_remove: os.rmdir(dirname)

注意:只要使用main_dir作为参数,就可以把它变成一个函数。

总结

以上是内存溢出为你收集整理的python,将所有文件从目录树的第3,第4,第5层移动到第2层全部内容,希望文章能够帮你解决python,将所有文件从目录树的第3,第4,第5层移动到第2层所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1154664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存