0

Essentially, I wan this function to take a file name, read the contents of that file into the profile_list. I want the list of profile objects to be returned when the function is called, but I can't seem to get this to work. Any help would be greatly appreciated

def read_file(filename, profile_list):

 infile = open(filename, "r")
 profile_list = infile.readlines()
 for i in range(len(profile_list)):
     profile_list[i] = profile_list[i].rstrip('\n')
 infile.close()
 return profile_list

profile_list = []
read_file("profiles.txt", profiles_list)

I am a beginner, and I do know I'm making a mistake somewhere, I just don't know where. The problem is that it's just not reading anything when called.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
riyalaise123
  • 43
  • 1
  • 1
  • 9
  • 1
    how do you call the code? What is `profile_list` in the function argument? (it looks like you immediately overwrite it) – Adam Smith May 22 '17 at 01:33
  • 3
    And what's wrong with the code you already have? What error are you getting? See [ask]. – Arya McCarthy May 22 '17 at 01:36
  • Two things to note, just as general guidelines. First, if you had put `print` statements inside your function you'd be able to see that it was reading the file (`print(profile_list)` after `profile_list = infile.readlines()` or etc) so the issue must be reading the information after it leaves the function. Secondly if you ever find yourself doing `for i in range(len(some_list)): do_something_with(some_list[i])` -- there's almost certainly a better way to write the code! – Adam Smith May 22 '17 at 01:50

4 Answers4

1
def read_file(filename, profile_list):

    infile = open(filename, "r")
    profile_list = infile.readlines()
    for i in range(len(profile_list)):
        profile_list[i] = profile_list[i].rstrip('\n')
    infile.close()
    return profile_list

read_file("profiles.txt", profiles_list)  # here is your only real mistake

You return profile_list, but then immediately throw it away when you don't assign the call of read_file(...) to anything.

result = read_file(...)

Now result has your profile list.

However do note that what you intended to do was to pass a pre-built list into the function and have it aggregate the results there. You can do that, but it's almost certainly not what you want to do. Just remove that line in the function definition.

def read_file(filename):
    # etc.

Or better yet name it something a little more clear. read_profiles maybe?

Also, you can strip each line while you read the file. Try this:

def read_profiles(fname):
    # this `with` construct is better than "f = open(...); do_stuff; f.close()" ...
    with open(fname) as infile:
        profile_list = [line.rstrip("\n") for line in infile]
    # ... because as soon as you exit the indented block, it closes for you.
    # EVEN if you exit the indented block because an error happened!
    return profile_list
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • Sorry to bother, but I tried your initial fix (the result = read_line() etc), but it still doesn't output anything from the file itself. I think I may be misunderstanding something. – riyalaise123 May 22 '17 at 01:58
  • 1
    the whole file should be read into `result` after that. Try printing `result` to console if that's where you're expecting to see it (`print(result)`) – Adam Smith May 22 '17 at 01:59
  • 1
    @riyalaise123 then I'm not sure what to tell you. Sounds like your file is empty! You can try printing each line inside the function `for line in profile_list: print(line)` but I'm sure it will come up empty as well. Make sure that you're using an absolute path to the file, or that your relative path starts from the right spot. Good luck! – Adam Smith May 22 '17 at 02:02
  • I mean it outputs something when result is called, but only "[]". Unsure of why this is. – riyalaise123 May 22 '17 at 02:04
  • The only improvement I'd suggest: instead of `[line.rstrip("\n") for line in infile]`, it seems to be that `infile.read().splitlines()` reads more simply. (I doubt there's any significant difference in performance, though I haven't tested it.) – CrazyChucky Nov 26 '20 at 03:17
  • @CrazyChucky `.read().splitlines()` is probably silly when there is a `.readlines()` :) – Adam Smith Nov 26 '20 at 05:03
  • @AdamSmith `splitlines()` automatically strips the newlines though, which is handy. Then you don't need the list comprehension. Tomayto, tomahto, I suppose. – CrazyChucky Nov 26 '20 at 05:05
0

Try this,

def read_file(filename):
    with open(filename) as f:
    return [i.rstrip('\n') for i in f]

read_file("profiles.txt")

Muthuraj S
  • 107
  • 6
0

Yes I think you returned value from read_file function and then set that to the profiles_list. like this

profile_list = read_file("profiles.txt", profiles_list)

profile_list which one you set outside of read_file function is not the same value with the on inside read_file

Jordan Micle
  • 59
  • 1
  • 8
-1
  1. Try to do the following in the caller of read_file:

    return_list = [] // define it a list
    read_file(..., return_list)
    
  2. inside read_file(), replace this line

    profile_list[i] = profile_list[i].rstrip('\n')
    

    with:

    return_list.append(profile_list[i].rstrip('\n'))
    

    In the end of this function, return the return_list to caller.

Pang
  • 9,564
  • 146
  • 81
  • 122
J.H.
  • 1
  • 1
  • this actually won't work, because before the `profile_list[i] = ...` lines, OP reassigns `profile_list` to `infile.readlines()`. Otherwise that would work, but isn't usually how Python handles this. – Adam Smith May 22 '17 at 01:47