Is there a good way to replace home directory with tilde in bash?

I don’t know of a way to do it directly as part of a variable substitution, but you can do it as a command: [[ “$name” =~ ^”$HOME”(/|$) ]] && name=”~${name#$HOME}” Note that this doesn’t do exactly what you asked for: it replaces “/home/alice/” with “~/” rather than “~”. This is intentional, since there are … Read more

File separators of Path name of ZipEntry?

The .zip file specification states: 4.4.17.1 The name of the file, with optional relative path. The path stored MUST not contain a drive or device letter, or a leading slash. All slashes MUST be forward slashes “https://stackoverflow.com/” as opposed to backwards slashes ‘\’ for compatibility with Amiga and UNIX file systems etc. If input came … Read more

Misunderstanding of python os.path.abspath

The problem is with your understanding of os.listdir() not os.path.abspath(). os.listdir() returns the names of each of the files in the directory. This will give you: img1.jpg img2.jpg … When you pass these to os.path.abspath(), they are seen as relative paths. This means it is relative to the directory from where you are executing your … Read more

Clean way to get the “true” stem of a Path object?

You could just .split it: >>> Path(‘logs/date.log.txt’).stem.split(‘.’)[0] ‘date’ os.path works just as well: >>> os.path.basename(‘logs/date.log.txt’).split(‘.’)[0] ‘date’ It passes all of the tests: In [11]: all(Path(k).stem.split(‘.’)[0] == v for k, v in { ….: ‘a’: ‘a’, ….: ‘a.txt’: ‘a’, ….: ‘archive.tar.gz’: ‘archive’, ….: ‘directory/file’: ‘file’, ….: ‘d.x.y.z/f.a.b.c’: ‘f’, ….: ‘logs/date.log.txt’: ‘date’ ….: }.items()) Out[11]: True