I have a few lines of code that should replace a string with a variable that contains multiple lines and if the variable contains nothing, then just replace the string with blank
My current file that has the string that should be replaced looks like
"resources": [
stringtobereplaced
]
My current code that replaces this is the following
with open('filepaths', "r+") as f:
for _ in range(1):
next(f)
for lines in f:
resourceslist = lines
print(resourceslist)
os.chdir(base_dir)
with open(unique_filename) as f:
newText=f.read().replace('stringtobereplaced', resourceslist)
with open(unique_filename, "w") as f:
f.write(newText)
The variable resourceslist has the following content in it.
"/home/django/copypaste/cleanup/var/media/admin/9514a8e4-8ad8-4917-a162-6d618b6616d3/splash279/lib/props/barbershop_pole.blend",
"/home/django/copypaste/cleanup/var/media/admin/9514a8e4-8ad8-4917-a162-6d618b6616d3/splash279/lib/props/hairdryer.blend",
"/home/django/copypaste/cleanup/var/media/admin/9514a8e4-8ad8-4917-a162-6d618b6616d3/splash279/lib/chars/pigeon.blend",
"/home/django/copypaste/cleanup/var/media/admin/9514a8e4-8ad8-4917-a162-6d618b6616d3/splash279/lib/chars/agent.blend",
"/home/django/copypaste/cleanup/var/media/admin/9514a8e4-8ad8-4917-a162-6d618b6616d3/splash279/lib/nodes/nodes_shaders.blend",
"/home/django/copypaste/cleanup/var/media/admin/9514a8e4-8ad8-4917-a162-6d618b6616d3/splash279/tools/camera_rig.blend",
but when I replace the string in my file with the variable resourceslist then it just outputs one line only. How do I manage to add all of them to the file or replace it with blank if the variable doesnt have anything in it.
Example of current output:
"resources": [
"/home/django/copypaste/cleanup/var/media/admin/089a4bd9-a618-41bd-a09b-f3616c773199/splash279/tools/camera_rig.blend",
]
for _ in range(1):? This is a 1-iteration loop, you can just execute the body without looping.