[PLUG] Python Lists and File Reads

Shahms King shahms at shahms.com
Fri Jul 15 23:08:38 UTC 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rich Shepard wrote:
|   I had a function working when I specified the input file as "*.py" and
| imported it as a list. However, I need to be able to use disk files with
| various names as input, so I changed the code. The function broke (of
| course!) and I cannot find a web page that gives me the appropriate
methods
| to use.
|
|   Here's the function code:
|
===========================================================================
| #!/usr/bin/python
| """
|   Takes a list of components and creates an output file of all pairwise
|   combinations of components.
| """
| import sys
| import pdb
|
| pdb.set_trace()
|
| def scopingpairs(input, output):
|
|   input = open('sys.argv[1]', 'r')
|   output = open('sys.argv[2]', 'a')
~                  ^^^^^^^^^^^^^
I think you probably want to get rid of the quotes around both  of those
~ arguments.  Additionally, what's the point of passing "input" and
"output" as parameters if you never actually use the values?

| src = input.read()

This will read the entire input into a single string.
You probably meant to say:

src = input.read().split()

(Which will split the input string into an array using whitespace as a
delimiter).

Additionally, (assuming I read your intention correctly) your loop
probably makes more sense as:

- ----------------------------------
for i,elem1 in enumerate(src):
~  for j,elem2 in enumerate(src):
~   if i < j:
~     print >>output, (elem1,elem2)
- ----------------------------------

Or, alteratively:

- ----------------------------------
for i,elem1 in enumerate(src):
~  for elem2 in src[i+1:]:
~    print >>output, (elem1, elem2)
- -----------------------------------

- --
Shahms E. King <shahms at shahms.com>
Multnomah ESD

Public Key:
http://shahms.mesd.k12.or.us/~sking/shahms.asc
Fingerprint:
1612 054B CE92 8770 F1EA  AB1B FEAB 3636 45B2 D75B
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFC2EH2/qs2NkWy11sRAiieAKCKnUFXrVwnT3xasu9rIllLvYuJTgCfZVm4
Zd9SPXf7KQ0z00Sz1QVUzAE=
=0CSL
-----END PGP SIGNATURE-----



More information about the PLUG mailing list