0

So I've seen this code;

with open(fname) as f:
 content = f.readlines()

in another question. I just need some confirmation on how it works. If I were to have a file named normaltrack.py which contains code;

wall 0 50 250 10
wall 0 -60 250 10
finish 200 -50 50 100

I should have a list called wall = [] and have the opening code as;

with open(normaltrack.py) as f:
 wall = f.readlines()

to open the file and store the lines of code that start with "wall" into the list?

Do I always have the change the "fname" everytime I want to open a different file? Or is there a way to do it from the interpreter? Such as python3 assignment.py < normaltrack.py ?

Ren
2,9563 gold badges28 silver badges50 bronze badges
asked Apr 8, 2016 at 3:02
5
  • You could pass an argument to the program that is the file name checkout stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script Commented Apr 8, 2016 at 3:07
  • readlines does not try and infer your data - you would need to parse each line to build a list Commented Apr 8, 2016 at 3:07
  • I'm new to programming (first-year uni student) so I don't really know what to do Commented Apr 8, 2016 at 3:09
  • Possible duplicate of Python: read file line by line into array Commented Apr 8, 2016 at 3:10
  • What exactly are you trying to do here? Are you trying to run code from two different files in the same interpreter? If so, readlines() is not meant for that. Commented Apr 8, 2016 at 3:12

3 Answers 3

1

In your example:

with open(fname) as f:
 content = f.readlines()

'fname' is a variable reference to a string. This string is the file path (either relative or absolute).

To read your example file, and generate a list of all lines that with 'wall', you can do this:

fname = '/path/to/normaltrack-example.txt' # this would be an absolute file path in Linux/Unix/Mac
wall = []
with open(fname) as the_file:
 for line in the_file:
 if line.startswith('wall'):
 wall.append(line) # or wall.append(line.rstrip()) to remove the line return character

In general, it's best to not call 'readlines()' on a file object unless you control the file (that is, it's not something the user provides). This is because readlines will read the entire file into memory, which sucks when the file is multiple GBs.

answered Apr 8, 2016 at 3:15
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a quick and dirty script that does what you want.

import sys
if len(sys.argv) > 1:
 infile = sys.argv[1]
else:
 print("Usage: {} <infile>".format(sys.argv[0]))
 sys.exit(1)
with open(infile, 'r') as f:
 walls = []
 for line in f:
 if line.startswith('wall'):
 walls.append(line.strip())

If you name this script 'read_walls.py', you can run it from the command line like this,

python read_walls.py normaltrack.py

Ordinarily, I'd use argparse to parse command-line arguments, and write a main() function for the code. (That makes it testable in the interactive python interpreter.)

answered Apr 8, 2016 at 3:19

Comments

0

this code should work for you

#!/usr/bin/env python
import sys
def read_file(fname):
 call = []
 with file(fname) as f:
 call = f.readlines()
 call = filter(lambda l: l.startswith('wall'), call)
 return call
if __name__ == '__main__':
 fname = sys.argv[1]
 call = read_file(fname)
 print call
answered Apr 8, 2016 at 3:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.