0

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",
]
2
  • Please fix your indentation, it's critical for Python. See formatting for code formatting help. Commented Dec 11, 2018 at 19:20
  • What's the point of for _ in range(1):? This is a 1-iteration loop, you can just execute the body without looping. Commented Dec 11, 2018 at 19:22

1 Answer 1

1

Iterating over a file object yields each line of the file, so your loop variable lines contains one line of your file at any given time. Each pass through the loop, you're overwriting the contents of resourceslist with the current value of lines, so at the end it contains the last line of the file.

If indentation doesn't matter, you can just set resourceslist = f.read() instead of the loop. If you want each line of your resources file to be indented the same way stringtobereplaced is, you're going to have to do a bit more complicated processing of the template file (maybe matching a regex like "^(?P.*)stringtobereplaced" and prefixing each resource line with the "prefix" group of the match object).

Sign up to request clarification or add additional context in comments.

2 Comments

f.read() does the work actually. Just need to find a way to replace with blank if none. Thank you very much for clarifying!
@LesnieSncheider If the variable is an empty string, then replace will just erase the matched bit (i.e. replace it with an empty string), so that part should already be done by the code you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.