1

I now have a file with a list of file paths. I want to loop open them to read and write. Can anyone suggest how to do this? Everything I have seen so far is only to read these lines and print them out, I want my code to open these paths. Below is a slice of the path file:

E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496340.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496341.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496342.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496343.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496344.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496345.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496346.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496347.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496348.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496349.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496350.txt
E:\Grad\LIS\LIS590 Text mining\Part1\Part1\awards_1994\awd_1994_96\a9496351.txt
...

This is the code I am trying:

def work_on(r'E:\Grad\LIS\LIS590 Text mining\file+test.txt'):  # The last quotation mark gives me that error. I also tried double quotation mark, didn't work either.
    with open(r'E:\Grad\LIS\LIS590 Text mining\file+test.txt', 'r') as data_file:

with open('file_list.txt', 'r') as file_list:    #file_list.txt is the file name I saved all the paths.
    for filename in file_list:
        with open(filename, 'r') as data_file:
            work_on(filename)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Q-ximi
  • 941
  • 3
  • 14
  • 21
  • My problem is I do not know which function(s) to use for this. Reading input and output now, but have not figured out if they are the right ones. – Q-ximi Mar 03 '14 at 15:21
  • The example by Sean Vieira below answers your question. – demo.b Mar 03 '14 at 15:23
  • Thanks for the clarification. I guess this is pretty basic, but I am not sure if I put the code together in the correct way. It keeps giving me an ERROR message: "SyntaxError: EOL while scanning string literal" for the single quotation mark behind the defline. I updated the code in my post. Thanks. – Q-ximi Mar 03 '14 at 19:01

1 Answers1

3

The general flow will be the same as printing out the lines - the only difference is instead of using print() you will do other work (in this case opening a file and working with it):

with open('/your/file/list.txt', 'r') as file_list:
    for filename in file_list:
        with open(filename, 'r') as data_file:
            # work with data_file here

You can then factor out the second piece of work into a separate function if that makes sense:

def work_on(data_file_path):
    with open(data_file_path, 'r') as data_file:
        # work with data_file here

which would then simplify your work loop to:

with open('/your/file/list.txt', 'r') as file_list:
    for filename in file_list:
        work_on(filename)
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • Thanks for your advice. I just started learning Python so there are a couple question I have about this piece of code. First, what is the difference between '/your/file/list.txt'and data_file_path you used above? Also, how do these pieces be put together? – Q-ximi Mar 03 '14 at 15:19
  • @Q-ximi - `your/file/list.txt` is the list of files that you provided a slice of in your question. When you iterate over an open file you get one line per iteration (e. g. `for line in file`) so `data_file_path` is one entry in your data list. – Sean Vieira Mar 03 '14 at 16:10
  • Thanks for the clarification. I guess this is pretty basic, but I am not sure if I put the code together in the correct way. It keeps giving me an ERROR message: "SyntaxError: EOL while scanning string literal" for the single quotation mark behind the `def`line. I updated the code in my post. Thanks. – Q-ximi Mar 03 '14 at 18:11
  • @Q-ximi - your function definition shouldn't have a string literal as it's parameter name :-) The point of `def` is you are defining a function that takes parameters that you will use in the body of the function. So `def work_on(some_param)`, not `def work_on('some_value')`. Does that make sense? – Sean Vieira Mar 03 '14 at 19:07
  • You should strip newlines and probably other whitespace: `filename = filename.strip()` – jfs Mar 03 '14 at 20:17
  • I guess my problem is I do not know how to nest these two pieces of code together. Think the error is because they are put in the wrong order or structure or something. This is driving me crazy! Can anyone show me how to get them work with each other? :-( :-( – Q-ximi Mar 03 '14 at 22:59
  • 1
    @Q-ximi - the third code block in my answer is how you would fit them together :-) You just need to fill out the `# work with data_file here` block. Try using blocks #2 and #3 in one file and just `print(data_file_path)` for `# work`. – Sean Vieira Mar 03 '14 at 23:04