我在MyFolder中有一堆文件,我需要像这样拆分它们:
MyFile.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之后,模块总是在file之前。
我的尝试没有成功,我设法获得了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')希望我的问题足够清楚。欢迎提出任何建议,谢谢!
发布于 2021-10-12 12:11:12
因此,经过一段时间后,我设法得到了我需要的东西,可能不是最好的解决方案,但它是有效的:
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('\\')https://stackoverflow.com/questions/69539416
复制相似问题