0

I'd like to add some words in a file using python script. Here is my script(for which name is test.py) and how I excute the script.

import os
some_python_words = "sys.path.insert(0, os.path.abspath('..'))\n"
scripts = "echo '%s' >> 'conf.py' " % (some_python_words)
os.system(scripts)

As I am execute in the terminal by python test.py. What writes in my file is

sys.path.insert(0, os.path.abspath(..))

The single quotation '' of .. is gone. What should I do?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
haojie
  • 593
  • 1
  • 7
  • 19

1 Answers1

2

This is because of the handling of single- and double-qoutes in the shell. A single-quoted string can't have interior single-quotes, but a double-quote can. See Difference between single and double quotes in Bash for a great writeup.

Just change your quoting when creating the shell string

scripts = """echo "%s" >> 'conf.py' """ % (some_python_words)
tdelaney
  • 73,364
  • 6
  • 83
  • 116