Asked  7 Months ago    Answers:  5   Viewed   31 times

Which Python library can I use to extract filenames from paths, no matter what the operating system or path format could be?

For example, I'd like all of these paths to return me c:




Using os.path.split or os.path.basename as others suggest won't work in all cases: if you're running the script on Linux and attempt to process a classic windows-style path, it will fail.

Windows paths can use either backslash or forward slash as path separator. Therefore, the ntpath module (which is equivalent to os.path when running on windows) will work for all(1) paths on all platforms.

import ntpath

Of course, if the file ends with a slash, the basename will be empty, so make your own function to deal with it:

def path_leaf(path):
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)


>>> paths = ['a/b/c/', 'a/b/c', '\a\b\c', '\a\b\c\', 'a\b\c', 
...     'a/b/../../a/b/c/', 'a/b/../../a/b/c']
>>> [path_leaf(path) for path in paths]
['c', 'c', 'c', 'c', 'c', 'c', 'c']

(1) There's one caveat: Linux filenames may contain backslashes. So on linux, r'a/bc' always refers to the file bc in the a folder, while on Windows, it always refers to the c file in the b subfolder of the a folder. So when both forward and backward slashes are used in a path, you need to know the associated platform to be able to interpret it correctly. In practice it's usually safe to assume it's a windows path since backslashes are seldom used in Linux filenames, but keep this in mind when you code so you don't create accidental security holes.

Tuesday, June 1, 2021
answered 7 Months ago

python3 is not Python syntax, it is the Python binary itself, the thing you run to get to the interactive interpreter.

You are confusing the command line with the Python prompt. Open a console (Windows) or terminal (Linux, Mac), the same place you'd use dir or ls to explore your filesystem from the command line.

If you are typing at a >>> or In [number]: prompt you are in the wrong place, that's the Python interpreter itself and it only takes Python syntax. If you started the Python prompt from a command line, exit at this point and go back to the command line. If you started the interpreter from IDLE or in an IDE, then you need to open a terminal or console as a separate program.

Other programs that people often confuse for Python syntax; each of these is actually a program to run in your command prompt:

  • python, python2.7, python3.5, etc.
  • pip or pip3
  • virtualenv
  • ipython
  • easy_install
  • django-admin
  • conda
  • flask
  • scrapy
  • -- this is a script you need to run with python [...].
  • Any of the above together with sudo.

with many more variations possible depending on what tools and libraries you have installed and what you are trying to do.

If given arguments, you'll get a SyntaxError exception instead, but the underlying cause is the same:

>>> pip install foobar
  File "<stdin>", line 1
    pip install foobar
SyntaxError: invalid syntax
Tuesday, June 1, 2021
answered 7 Months ago

For reference, here's the code I implemented, supposedly Win/Unix compatible:

    char *pfile;
    pfile = argv[0] + strlen(argv[0]);
    for (; pfile > argv[0]; pfile--)
        if ((*pfile == '\') || (*pfile == '/'))
Friday, July 30, 2021
answered 5 Months ago

use the following to convert to a timestamp in python 2


Sunday, August 22, 2021
answered 4 Months ago

Finder doesn't recognize all filename extensions. You could just use text item delimiters to remove the last part though.

do shell script "touch /tmp/some.file.eyetvr"
set text item delimiters to "."
tell application "Finder"
    set n to name of file (POSIX file "/tmp/some.file.eyetvr")
    if n contains "." then set n to (text items 1 thru -2 of n) as text
    n -- some.file
end tell
Monday, October 4, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :