0

This should be pretty easy, not sure why I can't get it to work. I am trying to import a ton of .txt files as part of a larger process like so:

    path = "C:/Users/A/B/"

    with open(path + "*full.txt","r") as f:
        contents =f.read()
        print(contents)  

I am just trying to import all .txt files (there are a ton of them) in this folder path, when I do this I get:

OSError: [Errno 22] Invalid argument: 

There are strings in the middle that are different between each file hence the * before the full it lists the path after argument (for privacy reasons I will leave it out but you get the point) and I know that the path is correct, why is it giving me this error?

bernando_vialli
  • 947
  • 4
  • 12
  • 27
  • Check this post: https://stackoverflow.com/questions/5013532/open-file-by-filename-wildcard – Ghoti Jul 26 '19 at 15:33
  • you can't use `*` in `open()`. `open()` can open only one file with exact name. You have to get all names in directory with `os.listdir(directory)` or `glob.glob("*file.txt")` and use `for`-loop to open every file separatelly. – furas Jul 26 '19 at 15:36
  • Possible duplicate of [Open file by filename wildcard](https://stackoverflow.com/questions/5013532/open-file-by-filename-wildcard) – wwii Jul 26 '19 at 15:45

2 Answers2

1

You can't use * in open(). open() can open only one file with exact name.

You have to get all names in directory and use for-loop to open every file separatelly.

With glob.glob():

import glob

path = "C:/Users/A/B/"

for fullname in glob.glob( path + "*full.txt" ):
    with open(fullname, "r") as f:
        contents = f.read()
        print(contents)

With os.listdir():

import os

path = "C:/Users/A/B/"

for name in os.listdir(path):
    if name.endswith("full.txt"):
        fullname = os.path.join(path, name):
        with open(fullname, "r") as f:
            contents = f.read()
            print(contents)
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you very much, but for some weird reason, this is taking forever to run without giving me any output even though I am testing it with only 3 files. Do you know why that might be? – bernando_vialli Jul 26 '19 at 16:49
  • actually semi never mind, it seems to work nice and quick with the 2nd approach with os but is running forever with glob – bernando_vialli Jul 26 '19 at 16:54
  • 1
    I never had problem with `glob` and it always work fast but if you use `*` in place of directory - `"C:/Users/*/full.txt"` then it will search in all subdirectores and it can take long time if there are many directores and files. With `listdir()` you always check only one directory. – furas Jul 26 '19 at 18:09
  • thank you, so this is part of a larger code that I have to figure out why it's not properly working, I thought if I fixed this the entire code would work apparently something is wrong, any chance you can take a look I have posted a new question here: https://stackoverflow.com/questions/57227712/issue-exporting-xml-files-with-python-code – bernando_vialli Jul 26 '19 at 22:54
0

When you type a wildcard at the command prompt like so:

cat /some/dir/*full.txt

The shell performs the wildcard expansion and passes the full actual filename to cat.

But Python doesn't do that; there is no shell. When you get to the point of calling open(), you must use the full real filename.

Try looking at the glob module.

John Gordon
  • 29,573
  • 7
  • 33
  • 58