每个元可以有1-5个文件,基本名称如下:
1) handling
2) vehicles
3) carvariations
4) carcols
5) dlctext
我在一个循环中检查这些基名中是否有一个在元文件中——如果为true,我将其添加到变量中
IDEA FOR THE FILES THATS IN THE FOLDER
name-handling.meta
name2-handling.meta
name2-carvarations.meta
name3-handling.meta
name3-dlctext.meta
name3-vehicles.meta
我需要一种方法来检查特定文件是否有一些arttibute(如代码中指定的),并且我想将数据分别写入_resource.lua文件和每个文件。示例如下:
The Code :
os.chdir(Paunch2 + '\\' + FolderCreatorName)
ResourceData = open('__resource.lua', 'x')
print('ResourceFile Were Openned')
ResourceData = open('__resource.lua', 'w')
ResourceData.write("resource_manifest_version '77731fab-63ca-442c-a67b-abc70f28dfa5'")
os.chdir(Paunch2 + '\\' + FolderCreatorName)
print('Entered the Folder :', Paunch2 + '\\' + FolderCreatorName, '\n\nLets Gooo We are inside the looping :D')
for dirpath, dirnames, files in os.walk('.') :
ResourceData.write("file {\n\n")
for file in files :
if file.endswith('.meta') and 'handling' in str(file):
print('meta')
HandlingFile = str(file)
ResourceData.write(f"'{HandlingFile}',\n")
print(f"data file 'HANDLING_FILE' '{HandlingFile}'")
elif file.endswith('.meta') and 'vehicles' in str(file):
VehiclesFile = str(file)
ResourceData.write(f"'{VehiclesFile}',\n")
print(f"data file 'VEHICLE_METADATA_FILE' '{VehiclesFile}'")
elif file.endswith('.meta') and 'carvariations' in str(file):
CarVariationsFile = str(file)
ResourceData.write(f"'{CarVariationsFile}',\n")
print(f"data file 'VEHICLE_VARIATION_FILE' '{CarVariationsFile}'")
elif file.endswith('.meta') and 'carcols' in str(file):
CarcolsFile = str(file)
ResourceData.write(f"'{CarcolsFile}',\n")
print(f"data file 'CARCOLS_FILE' '{CarcolsFile}'")
elif file.endswith('.meta') and 'dlctext' in str(file):
DLCTextFile = str(file)
ResourceData.write(f"'{DLCTextFile}',\n")
print('Dlctext is working')
elif file.endswith('.meta') and 'vehiclelayouts' in str(file):
LAYOUT = str(file)
ResourceData.write(f"'{LAYOUT}',\n")
print(f"data file 'VEHICLE_LAYOUTS_FILE' '{LAYOUT}'")
Output in the File that printing in :
resource_manifest_version '77731fab-63ca-442c-a67b-abc70f28dfa5'file {
'f777-carvariations.meta',
'f777-handling.meta',
'f777-vehicles.meta',
'superkart-carcols.meta',
'superkart-carvariations.meta',
'superkart-handling.meta',
'superkart-vehicles.meta',
'wmfenyr-carcols.meta',
'wmfenyr-carvariations.meta',
'wmfenyr-dlctext.meta',
'wmfenyr-handling.meta',
'wmfenyr-vehicles.meta',
file {
IT HAVE TO BE LIKE :
file {
'f777-carvariations.meta',
'f777-handling.meta',
'f777-vehicles.meta',
}
file {
'superkart-carcols.meta',
'superkart-carvariations.meta',
'superkart-handling.meta',
'superkart-vehicles.meta',
}
file {
'wmfenyr-carcols.meta',
'wmfenyr-carvariations.meta',
'wmfenyr-dlctext.meta',
'wmfenyr-handling.meta',
'wmfenyr-vehicles.meta',
}
我该怎么做才能让它变成这样
解释
您发布的代码存在许多问题:
open('__resource.lua', 'x')
可能会引发FileExistsError
,但根本不需要它,因为使用"w"
模式打开的文件在不存在时会自动创建Paunch2 + '\\' + FolderCreatorName
两次,这是不必要的"file {\n\n"
被添加到"resource_manifest_version"
的顶部,因为您没有在写入的末尾添加换行符(对于"resource_manifest_version"
)"file {"
之后添加两个换行符,这不是您想要做的事情-它将添加两个新行而不是一个新行__resource.lua
写入文件名时,都没有对其进行缩进(没有使用"\t"
字符)"}\n"
,这样就可以有一个右括号以上所有操作将导致您的代码如下所示:
现在,从我看来,这不是你想要的。您希望将文件名分隔为不同的
file { }
解决方案
为了做到这一点,我们首先需要按名称分隔所有文件名,只有在我们查看了所有文件之后,我们才能开始写入
__resource.lua
(除非您确定您的文件系统对文件名进行了排序)。我们可以使用dict
存储名称,因为键和值可以是文件名列表:如果文件系统自动对文件名进行排序,我们只需检查当前文件名是否与上一个文件名相同-如果相同,则只需将文件添加到当前
file { }
,否则关闭当前file { }
并启动一个新文件:相关问题 更多 >
编程相关推荐