-1

I am currently in the process of writing a script that will parse a file for a specific string and rename the file to that string. However, the files that this script will be dealing with do not have file extensions. They are readable in any text editor, but they do not have a file type other than the generic "FILE" type in their properties window.

I have tried researching this for a few days now and have been unsuccessful in finding any documentation specific to my problem. So is there any python method of opening, reading, renaming, and saving a file that has no specified file extension?

armatita
  • 12,825
  • 8
  • 48
  • 49
Dima
  • 3
  • 2
  • Have you tried os.rename()? – flyingmeatball May 16 '16 at 17:58
  • Why do you think that having an extension matters? – Roland Smith May 16 '16 at 18:01
  • _is there any python method of opening, reading, renaming, and saving a file that has no specified file extension?_ yes, same function as for manipulating files with extensions. See [File objects](https://docs.python.org/2/library/stdtypes.html#file-objects) and [os](https://docs.python.org/2/library/os.html) module. – Łukasz Rogalski May 16 '16 at 18:02
  • @RolandSmith My original attempt at this used the 'open' method for opening and reading the file. I've tried passing in a string that matches the name of my test file (without an extension) into my script, but it fails to find the file. I have run the same script looking for .txt files within the same directory, and this produces expected results. – Dima May 16 '16 at 18:10
  • Ah. Maybe the extension contains whitespace? If you use `os.walk` as I've shown below, it *will* find all files. – Roland Smith May 16 '16 at 18:14
  • @RolandSmith I ended up implementing a combination of what you showed me as well as the function from Luis Morales. I think your assertion that the extension contained whitespace may have been the cause of the problem in finding these files. Anyway, I now have a working script that and ensures every file in the directory is named correctly based on my parser rules. Thanks everybody! – Dima May 16 '16 at 18:37

2 Answers2

0

Have a look at this answer.

When you iterate through the list of files you can simply check if the extension is empty.

def file_has_no_extension(file_path):
    """
    Return true if and only if the file has no extension.
    """
    filename, file_extension = os.path.splitext('/path/to/somefile.ext')

    return not file_extension

For the rest of your question just have a look at tutorials like this.

Community
  • 1
  • 1
Luis Morales
  • 96
  • 1
  • 8
0

You can open() or os.rename() all files that you have access to. It doesn't matter if it doesn't have an extension.


If you don't know which files to rename, then just open all files, read their contents and act on the files that match what you're looking for.

import os

# Get a list of all files under the current directory
flist = []
for root, dirs, files in os.walk('.'):
    flist += [os.path.join(root, f) for f in files]

# Go over each file and see if it contains the string foo
want = 'foo'
targets = []
for path in flist:
    with open(path) as df:
        data = df.read()
    if want in data:
        targets.append(path)
# The targets list now contains the paths of all the files that contain foo.
Roland Smith
  • 42,427
  • 3
  • 64
  • 94