2

I am writing a script in python that will read in data from a text file and perform various operations on the data. It is working perfectly if I specify the extension of my file in this statement with open("file.txt") as f:. My question is (and I don't even know if this is possible), I have to open a file that someone will provide to me but I let's assume that they will all provide it with different extensions (.txt, .rtf, .odt, .doc). All of the files will have the same layout which is for example:

14 Lucas,13 Markus,7 Peter,4

So essentially:

integer
string,integer
string,integer
string,integer

Is there a way to set the extension in with open("file.txt") as f: to something like with open("file.*") as f: that will process all the text files apart from their extension, given they have all the same layout.

user3245453
  • 251
  • 1
  • 5
  • 10
  • The problem with `.rtf`, `.doc`, etc. files is that they're **NOT** text files. They may look the same on screen when viewing them with the correct program, but if you were to open them in a plain text editor, you'd see what their contents really are... – MattDMo Jan 29 '14 at 19:41
  • @MattDMo I think those were just examples. – Hyperboreus Jan 29 '14 at 19:42
  • 1
    @OP: Take a look at `os.walk()` and other directory listing features in the `os` module. – Hyperboreus Jan 29 '14 at 19:42
  • The `glob` module may also help you. – Wooble Jan 29 '14 at 19:44

3 Answers3

0

You need to give open a full path. The extension may have signification on Windows, but this is just an artifact, it is actually part of the file name. However, you can use listdir and fnmatch to get all files in a directory matching a specific pattern.

Or as Wooble said, you can use the glob module. The following example takes a path, removes the extension to replace it with a star and create a search pattern for glob. Then it uses glob to get a list of matching files and loops over all files to apply whatever algorithm you want.

>>> import glob
>>> import os.path
>>> path = "/tmp/test.txt"
>>> root, ext = os.path.splitext(path)
>>> pattern = root + ".*"
>>> list_of_files = glob.glob(pattern)
>>> list_of_files
['/tmp/test.rtf', '/tmp/test.txt']
>>> for f in list_of_files:
        with open(f, "r") as ...
Cilyan
  • 7,883
  • 1
  • 29
  • 37
0

Such functionality can be implemented using th glob module.

import glob
print glob.glob('*.*')

Will print every file in the directory.

import glob
print glob.glob('*.txt')

Will print every text file in the directory.

The one you are looking for is :

import glob
print glob.glob('name_of_the_file.*')

So you can do :

    import glob
    for i in glob.glob('name_of_the_file.*') :
        with open(i) as f :
      # do operations
Azwr
  • 774
  • 8
  • 13
0

This is just a workaround. May not work in some cases. What I am trying to do is get list of all the files and search for required name.

file_name='YOUR_FILE_NAME'+'.'
l=os.listdir(PATH_TO_FILE)
for f in l:
    if f.startswith(file_name):
        fname=f
with open(fname) as f:
    #perform operation
santosh-patil
  • 1,540
  • 1
  • 15
  • 29