-1

I was working on a script that does something with the files in a given folder. Put some checks in place to assure that the path given (as a string) is a directory path and is absolute.

if not (os.path.isdir(dirpath) and os.path.isabs(dirpath)):
    error_message = f"Path given: \"{dirpath}\" :was inappropriate for required uses."
    main_logger.error(error_message)
    raise Exception(error_message)

But while testing it on a folder, I got some unexpected results. The folder contained some pdfs in it and the function only extracts the "files" in a folder and ignores any subfolders.

file_list: list[str] = [os.path.join(dirpath, file) for file in os.listdir(dirpath) if os.path.isfile(file)]

But it ignored all pdfs and marked them as "not files". So, how can I check if a path is any file and not just a regular file? I can check if it has an extension or not. But I don't have any good method of doing that.

Checked some other ways to do it, for example:-

pathlib.Path(filepath).is_file()

But that didn't work either.

I have now settled for checking if it is not a directory path. But it could be useful to know about any other ways.

So, any way to do it?

Edit: Difference between any file and a regular file:-
Any file: Any file means that a file with any extension(s). Ex:- main.py, test.h etc.
Regular file: I used the term "regular file" as that is how they are described in the official documentation. A possible definition could be here.

And the pathlib.Path(filepath).is_file() method "didn't work" meant that it produced the exct same result as the os.path.isfile() method.

Also, I don't want to check for a specific extension either. I want it to work for any file.

NewHere
  • 61
  • 1
  • 8
  • Can you clarify the difference, between any file and a regular file, when saying "So, how can I check if a path is any file and not just a regular file?" ? – Luuk Sep 19 '21 at 08:33
  • 1
    Try this first: https://stackoverflow.com/questions/33998802/get-list-of-pdf-files-in-folder – BlackFox Sep 19 '21 at 08:36
  • Does this answer your question? [how to check if a file is a directory or regular file in python? (duplicate)](https://stackoverflow.com/q/3204782/6045800) – Tomerikoo Sep 19 '21 at 08:50
  • Does this answer your question? [How can I check the extension of a file?](https://stackoverflow.com/q/5899497/6045800) – Tomerikoo Sep 19 '21 at 08:50
  • "regular file" is in contrast to stuff like directories or named pipes. PDFs are regular files. If you didn't get the PDFs you were looking for, something else has gone wrong. – user2357112 Sep 19 '21 at 09:10
  • 1
    That something else is most likely the fact that you called `isfile` with just a bare file name instead of the actual path to the file. – user2357112 Sep 19 '21 at 09:12
  • To help clarify your question I suggest taking an example file (say filename.txt) and pasting the output of Linux commands `file filename.txt` and/or `stat filename.txt` as well as the output of `pathlib.Path('filename.txt).is_file()` and explain what was expected and why. – Nagev Sep 19 '21 at 09:15
  • 1
    So, yeah, I was not passing the actual path in the function. Sorry for the inconvenience. My bad. – NewHere Sep 19 '21 at 13:26

2 Answers2

1

you could try

file_list = [os.path.join(dirpath, file) for file in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, file))]

pippo1980
  • 2,181
  • 3
  • 14
  • 30
-3

You can simply use .endswith() method.

if file.endswith('.py'):
    ...
    enter code here
else:
    ...