0

I've been struggling to open some files in python from a text file with a list of strings that are file names: for example the .txt file would contain a list of names like the following r'c:\users\computer\documents\example.txt'

I want to run a loop that looks at each line and sees if 'example' is in the line. If so I want to open(line), however I cannot, and get a OSError: [Errno 22] Invalid argument:

If I manually type the file name in I can open it, but the variable will not open it. I tried assigning a variable to line, replacing '\n' trying StringIO, but getting nowhere. I'm assuming its because python is looking at the variable as a string object and needs a file object. But how do I make the string line into a file object of the same name. Thanks

Kedar Kodgire
  • 482
  • 6
  • 22
  • Post your code! have you removed the newline? `open(line.strip())` may work better. And is that .txt file holding literally `r'c:\users\computer\documents\example.txt'` or `c:\users\computer\documents\example.txt` – tdelaney Apr 10 '17 at 18:01
  • dates = ['2016q4', '2016q3', '2016q2', '2016q1'] for date in dates: with open('c:\\Users\Work Computer\My Documents\SUB_FILES.txt') as filelist2: for line in filelist2: if date in line: with open(line.strip()) as sub_file: for line in sub_file: split1 = line.split() if line.startswith(cik): Nl = line.split() adsh.append(Nl[1]) else: pass print(adsh) – Mudfoot Brown Apr 10 '17 at 18:10
  • dates = ['2016q4', '2016q3', '2016q2', '2016q1'] for date in dates: with open('c:\\Users\Work Computer\My Documents\SUB_FILES.txt') as filelist2: for line in filelist2: if date in line: with open(line.strip()) as sub_file: for line in sub_file: split1 = line.split() if line.startswith(cik): Nl = line.split() adsh.append(Nl[1]) else: pass print(adsh) – Mudfoot Brown Apr 10 '17 at 18:13
  • trying to post the code – Mudfoot Brown Apr 10 '17 at 18:13

2 Answers2

0

The open function accepts str arguments, so to do what you are describing you would do something like

with open(r'names.txt') as examples:
    for line in examples:
        if 'example' in line:
            with open(line.strip()) as example_file:
                # do something with example_file

So each example_file would be opened one-by-one based on the lines in names.txt.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • I think that's `line.strip()` – tdelaney Apr 10 '17 at 18:02
  • still getting error. the .txt file looks like this – Mudfoot Brown Apr 10 '17 at 18:15
  • r'C:\\Users\WorkComputer\Downloads\2016q4\sub.txt r'C:\\Users\WorkComputer\Downloads\2016q3\sub.txt r'C:\\Users\WorkComputer\Downloads\2016q2\sub.txt r'C:\\Users\WorkComputer\Downloads\2016q1\sub.txt r'C:\\Users\WorkComputer\Downloads\2015q4\sub.txt – Mudfoot Brown Apr 10 '17 at 18:15
  • Do the lines really contain `r'...`? Why the raw string prefix? – Cory Kramer Apr 10 '17 at 18:17
  • because usually when i open the file thats how i would type it – Mudfoot Brown Apr 10 '17 at 18:18
  • 1
    Try having the file content look like `C:\Users\WorkComputer\Downloads\2016q4\sub.txt` with no quotes and no `r` prefix. There is a big difference between how strings are represented and their actual content. https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l – Cory Kramer Apr 10 '17 at 18:19
0
with open('names.txt', 'r') as examples:
search = str(input('Who would you like to find?: '))
for line in examples:
    if search in line:
        print('Searching...')
        print(search, 'is here!')

Im still learning python myself but that should check every line for a name entered and print if found or not. And you can of course change to fit your needs. Let me know if this helped!!!