0

I am trying to open a json file to write, but i need to build the file path with combination of variable value + some string

What I tried:

  with open(r"c:\csv\{ip}+aws_get_tags.json", "w") as f:
        json.dump(ec2_tags, f, indent=4, separators=(',', ': '))

this is erroring out at opening that line.

Can some one let me know what syntax i am missing

asp
  • 777
  • 3
  • 14
  • 33
  • 1
    ```"c:\csv\{ip}+aws_get_tags.json".format(ip = )```. – sushanth Jul 21 '20 at 06:35
  • 1
    You can use *format* strings: `f"{ip}"` etc where `ip` is a variable available in the scope. (So just replace `r` with `f` ...). Also, try to use `os.path.join`. – Nishant Jul 21 '20 at 06:38
  • Hi suhanth, thanks for solution but that. in format is that manual entry ?, actually ip is variable which will pass into this script .any way let me try – asp Jul 21 '20 at 06:39
  • 1
    @asp, it's just a illustration replace ```` with the varaible or you can use one recommened by @Nishant – sushanth Jul 21 '20 at 06:41
  • 1
    Does this answer your question? [How do I put a variable inside a string?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-inside-a-string) – Nishant Jul 21 '20 at 06:41
  • 1
    Nishant Sushanth thnaks for inputs putting 'f' solved the issue – asp Jul 21 '20 at 06:47

0 Answers0