Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:33:52+00:00 2026-06-15T05:33:52+00:00

I am trying to write a script file via a Python script For that

  • 0

I am trying to write a script file via a Python script

For that I have defined a function to open, write & close file
def funcToCreateScript(filename,filecontent)

I have another function, where I am calling funcToCreateScript and am trying to pass it shell script content through a variable. The problem is when I format the text as below

def createfile():
    var = """#!/bin/sh
             echo ${test}
          """
    funcToCreateScript(filename,var)

I get the output of script as –

#!/bin/sh
         echo ${test}

So, its taking the function’s indentation and writing it accordingly. Is there a way to format it so that it will look like

#!/bin/sh
echo ${test}

e.g. –

def main():
    var = """
        #!/bin/sh
        echo ${test}
        """
    print var

main()

 > test.py

                #!/bin/sh
                echo ${test}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:33:53+00:00Added an answer on June 15, 2026 at 5:33 am

    There are two solutions here, either use the new line escape character (‘\n’), so it would look like:

    def createfile():
        var = """#!/bin/sh\necho ${test}"""
        funcToCreateScript(filename,var)
    

    Keep in mind, if you use this technique (which is pretty nasty solution – see below for a better alternative), and you want to see the output you’ll need to print() (assuming Python 3.0 standards here).

    Or, just simply do:

    def createfile():
        involved. 
        var = """#!/bin/sh
    echo ${test}
              """
        return funcToCreateScript(filename,var)
    

    I am not sure how you got funcToCreateScript setup, but here is some trial code so you can see it in action (copy and paste into an interactive interpreter or new python file and run):

    def createfile():
        var = """
    #!/bin/sh
    echo ${test}
              """
        return var
    
    v = createfile()
    

    If you want to get fancy, but still make it readable you can use lstrip(” “) which will remove all white space from the left side.

    def createfile():
        var = """#!/bin/sh
                 echo ${test}
              """
        script_txt = ""
    
        for line in var:
            script_txt += line.lstrip(" ")
    
        return script_txt
    
    v = createfile()
    

    Just be weary of the last solution because if you have whitespace that you intended to have there, then it’ll strip that away (I personally like option 2 I listed).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a script that will open a file in vim
I'm trying write a python 2.7 script, that connects to a server via SSL
I am trying to write a script which uploads a file via a html
I'm trying to write a little Emacs Lisp script that reads a csv file
I'm trying to write a script that breaks up a VERY large file into
I'm trying to write a script that will search through a html file and
I am trying to write a shell script to simulate the following problem: File
I'm trying to write a script with wsadmin that will retrieve the total amount
I'm trying to write a script that will create a user in MediaWiki, so
I'm trying to write a script that allows an admin of a photo uploading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.