Wildcard support in filenames for Windows #25
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
By ComFreek on 2015-11-17T11:14:13.068Z
Currently, the following command fails on Windows because cmd.exe and PowerShell do not perform file name expansion, as opposed to most *NIX shells.
python.exe -m img2pdf myFolder/*.jpg -o output.pdf
PS: You may be interested in the post and its comments on StackExchange where this feature request was initially proposed: http://softwarerecs.stackexchange.com/a/26102/583
By josch on 2015-12-09T13:17:11.312Z
Thanks! Unfortunately I do not have access to any Windows machine to test any such feature.
A pull request from anybody who can test a patch enabling this would be most welcome.
By josch on 2017-01-21T07:54:46.128Z
Closing due to lack of activity from original submitter.
By josch on 2017-01-21T07:54:46.492Z
Status changed to closed
By Matlock42 on 2017-04-18T22:26:52.651Z
I am willing to help troubleshoot this issue, however, I have only limited knowledge of python.
The following is the error I get when trying to run with a wildcard on windows:
A little bit of searching found this stack exchange post that looks like it might address the issue: http://stackoverflow.com/questions/12501761/passing-multple-files-with-asterisk-to-python-shell-in-windows
Specifically, the following snippet looks promising:
However, I don't really know how to specifically implement this into your python script.
By Matlock42 on 2017-04-18T22:37:39.612Z
As a workaround, I created a batch script that allows you to drag and drop images onto the batch file and it will run the python script with each file called out in quotes. You can change the script call at the end based on your needs.
By josch on 2017-04-20T05:05:27.588Z
My argument here is the following:
When I type
program *.ext
on a typical Linux shell then the shell will expand*.ext
into all the files matching the glob expression.When I type the same in the
cmd.exe
that Windows ships, then it will try to find a file named exactly*.ext
(including the asterisk) which doesn't exist and isn't even a valid filename on Windows (the asterisk character is not allowed).So expanding a glob into the files it matches is the task of the shell and not the task of the program you execute. So my first thought about that is, that if you want a globbing expression to be expanded into the files it matches, then you should just start using a more powerful shell than
cmd.exe
.Think about it also the following way: if I now integrate the following code snippet you proposed:
Then that would mean that even under more powerful shells like the ones you typically find on Linux systems, you could not pass files to img2pdf anymore that contain an asterisk because then filenames including an asterisk would always be treated as if they were meant as globs. So adding code like the above would actually remove functionality for users.
Though, surely, above code snippet could be made specific to Windows and not be executed for users on Linux. But since this is a problem with the shell you run img2pdf from, it's not the right way to make this dependent on the operating system. For example imagine a user on Windows who is actually using a powerful shell that allows them to use globbing. Then those users would have functionality removed.
So instead of moving functionality that your shell should provide into img2pdf, it would be far easier and less painful for anybody else if those users who need globbing to work would just use a shell that supports globbing.
If you disagree with my arguments I am open to hear yours.
By Matlock42 on 2017-04-21T22:10:45.995Z
I have been contemplating your response to this and doing a bit of research. From what I can tell windows has always relied on the programs to work out globbed arguments when they are passed (source). This is true for both windows shells
cmd
andPowerShell
(ps). I tested img2pdf with both shells and got the same error. Not to say that there are not ways around this by making a shell script to do the work and pass it correctly into the program. But that adds another layer outside the python program.Conversely, your argument is valid from the fact that it makes the program more complicated for only a portion of the potential userbase to see an advantage. However,
img2pdf --help
does list using a globbing argument as valid syntax and the result of said syntax on windows using either cmd or ps results in an error. That is why I commented on this issue.My opinion is to have the snippet run on windows only and not executed for linux users.
By josch on 2017-04-23T07:06:27.410Z
I checked the superuser source you cite but there people say that globbing works with powershell. Still you claim that it doesn't. Can you explain which information is the correct one?
The
img2pdf --help
text shows*.jpg
in the example section. But it also lets each line start with a dollar sign which is an indicator for a POSIX shell. Otherwise you could also argue that it's wrong to list justimg2pdf
as the right command in the example section because under windows you'd have to callpython img2pdf
instead. So if this is your complaint, then what you actually want to report is that theimg2pdf --help
output should list examples for different shells. So that there would be examples forbash
as well as forcmd.exe
. If that's what you are argueing, please open a separate bug about this issue. This issue is about wildcard support for img2pdf.By Oliver on 2019-02-09T21:53:47.997Z
The behavior of the shell on Unix is rather special. It takes care of all the expansion of shell globs. For Windows programs this can be sort of emulated by linking against a special object file provided by Visual Studio. You can find detailed instructions here. This allows for easier porting of POSIX/SUS programs. Hope this helps.