faqts : Computers : Programming : Languages : Python : Snippets : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 10 people (50%) answered Yes
Recently 4 of 9 people (44%) answered Yes

Entry

Substituting strings

Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 10, Piet van Oostrum


"""
Packages: basic_datatypes.strings
"""

"""
DR> Hi,
DR> In this example, how do I get the shell to recognize the value 
DR> for file[0] rather than the literal string 'file[0]'?  Please email me...

DR> -------------------------------------------------------------------------
DR> import sys, os

DR> file = sys.argv[1:]

DR> print file[0]

DR> for name in os.popen("dir file[0]", 'r').readlines():
DR>    print name,
DR> --------------------------------------------------------------------------
"""

file = ["myfile", "yourfile"]  # added by PSST

print "dir " + file[0]

print "dir %s" % file[0]  # alternative

"""
But in this example it may be better to use the os.listdir function rather
than calling the external dir program.
"""