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

  • SEARCH
  • Home
  • 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 7757447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:05:51+00:00 2026-06-01T13:05:51+00:00

I’m experimenting with file I/O. I have a small practice program that creates a

  • 0

I’m experimenting with file I/O. I have a small practice program that creates a text file when run. I packaged it with pyinstaller so that double clicking on the exe creates a new folder and places a text file with “hello world” inside of it. Easy peasy.

Then I started wondering about main(). This is just a function like any other, right? So does that mean I can pass arguments to it at runtime?

I was thinking about the Steam client and how you can put stuff like ‘-dev’ and ‘-console’ in the shortcut. Is there a way to do this to a python exe that I have made?

I may be explaining terribly, so here’s an example:

def makeFile(string):
    if string:
        f = open('mytext.txt', 'w') #create text file in local dir
        print >> f, 'hello, ' + string + '! \nHow are ya?'
        f.close()
    else:
        f = open('mytext.txt', 'w') #create text file in local dir
        print >> f, 'hello, person! \nHow are ya?'
        f.close()

def main(string = None):
    makeFile(string)     

So if I take this code and make it an exe, would I be able to add my optional arguments somehow.

I tried the above code, and the running test.exe --"myname" but that didn’t work.

Is there a way to do this?

  • 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-01T13:05:52+00:00Added an answer on June 1, 2026 at 1:05 pm

    Yes, you can do it with sys.argv. Check out this link: http://docs.python.org/library/sys.html#sys.argv. But remember not to forget import sys, and then you can use it.
    import sys

    # If there is an argument passed to your file
    if len(sys.argv) > 1:
        # argv[1] has your filename
        filename = sys.argv[1]
        print (filename)
    
    # Output...
    # new-host:~ yanwchan$ python3.2 test.py text.txt
    # text.txt
    

    argv[0] has test.py

    argv[1] has text.txt

    Edit: However, I do some more research on this topic and found out this: https://stackoverflow.com/a/4188500/1276534

    As katrielalex points out, maybe you can look into argparse as well.? It provides a lot more functionality as well as safety check. Interesting information.

    And here is a great tutorial: http://www.doughellmann.com/PyMOTW/argparse/

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

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.