flickr-download icon indicating copy to clipboard operation
flickr-download copied to clipboard

Convert set and photo names to valid filenames

Open astanin opened this issue 6 years ago • 3 comments

To avoid exceptions when creating directories and saving files, only valid filenames should be used. For portability reasons Windows filename restrictions are enforced: https://stackoverflow.com/a/31976060/25450

Reserved characters are replaced with an underscore '_'. Trailing whitespace and dots are removed. Reserved filenames (like COM or NUL) are prefixed with an underscore.

astanin avatar Nov 14 '18 22:11 astanin

Thanks for the PR! I have a few code comments which I will add in a bit. But is there really not built in way to do this in Python? Ugh. As much as I hate operating system specific code, let's at least only do it on Windows. There is no reason to change the filename on other operating (/file) systems with less restrictions.

beaufour avatar Nov 16 '18 13:11 beaufour

I implemented the changes, mostly as you asked. I moved regular expressions to the global scope. So the same compiled expressions can be reused most of the time.

But is there really not built in way to do this in Python?

Not that I know of.

There is no reason to change the filename on other operating (/file) systems with less restrictions.

For a long time I was doing all my photo management in Linux, and eventually I found that it's better to follow the most restrictive file naming conventions, because sooner or later there might be a need to copy files to a Windows filesystem. Basically, just avoiding <, >, ", :, slashes, *, ?, and | in the filenames makes any archive much more portable.

If you want I may add platform detection checks, but if I were you I would keep the script as simple as possible and choose only one behavior for all platforms.

you might also want to treat filenames and directories differently

According to Microsoft docs, naming rules are the same for filenames and directories.

astanin avatar Nov 19 '18 22:11 astanin

Sorry, this got lost.

I implemented the changes, mostly as you asked. I moved regular expressions to the global scope. So the same compiled expressions can be reused most of the time.

But is there really not built in way to do this in Python?

Not that I know of.

Kind of crazy.

There is no reason to change the filename on other operating (/file) systems with less restrictions.

For a long time I was doing all my photo management in Linux, and eventually I found that it's better to follow the most restrictive file naming conventions, because sooner or later there might be a need to copy files to a Windows filesystem. Basically, just avoiding <, >, ", :, slashes, *, ?, and | in the filenames makes any archive much more portable.

If you want I may add platform detection checks, but if I were you I would keep the script as simple as possible and choose only one behavior for all platforms.

I am in favor of platform detection. No reason to limit users on all operating systems because of one platform's restrictions.

you might also want to treat filenames and directories differently

According to Microsoft docs, naming rules are the same for filenames and directories.

Ok. As long as it's platform specific, then all good.

beaufour avatar Dec 23 '18 15:12 beaufour

I added https://pypi.org/project/pathvalidate/ a while back

beaufour avatar Sep 20 '22 22:09 beaufour