![](/img/trans.png)
[英]Modify all files in specified directory (including subfolders) and saving them in new directory while presevering folder structure (Python)
[英]Python parse all lines inside of a files and split them by folder structure
我的 MyFolder 中有一堆文件,我需要以某种方式将它们拆分如下:
我的文件.txt:
C:/Storage/agent/temp/6620/sw/main/folder/Module/Types.h
C:/Storage/agent/temp/6620/sw/main/folder/Module2/Types1.h
C:/Storage/agent/temp/6620/sw/folder/folder/Module3/Type.h
etc..
我需要将所有行拆分为这种格式,因为 temp (6620) 之后的这个文件夹正在发生变化,而且“sw”之前的所有内容都可能发生变化,所以我想将它们作为一个部分,以便我可以做其他事情“右侧”的其他人,输出应为:
BUILD_DIRECTORY(C:/Storage/agent/temp/6620)+sw(sw)+base_folder(main)+folder(folder)+module(Module)+file(Types.h)
重点是 sw 将始终在 BUILD_DIRECTORY 之后,基本文件夹始终在 sw 之后,模块始终在文件之前。
我的尝试没有成功,我设法获得了 BUILD_DIRECTORY 和 REST,但是 sw 总是丢失,我想有更好的方法来做到这一点:
for filename in files:
with open(filename, 'r') as file:
content = file.read().split('\n')
for line in content:
if not line: continue
# Seperate full path to the module in variables
BUILD_DIRECTORY, REST= line.split('/sw')
希望我的问题足够清楚。 欢迎任何建议,谢谢!
所以一段时间后,我设法得到了我需要的东西,可能不是最好的解决方案,但它有效:
for filename in files:
with open(filename, 'r') as file:
content = file.read().split('\n')
for line in content:
if not line: continue
# Seperate full path to the module in variables
BUILD_DIRECTORY, SW= line.split('sw/')
sw+base_folder+folder+module+file = sw.split('\\')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.