I have a script that I want to use in order to enumerate all file types in a folder:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import argparse
import magic
import os
# Argparse starts here
parser = argparse.ArgumentParser()
parser.add_argument('-input', dest='input',help="input one or more files",nargs='+',metavar=None)
args = parser.parse_args()
for files in args.input:
if magic.from_file(files,mime=True) == "text/plain":
print (files, "=" , magic.from_file(files,mime=True) )
it works pretty well when I input a file:
even when I input two files:
but not when I input ALL files:
The error says:
Traceback (most recent call last):
File "Test.py", line 15, in <module>
if magic.from_file(files,mime=True) == "text/plain":
File "C:\Users\FrancescoM\AppData\Local\Programs\Python\Python36-32\lib\site-packages\magic\magic.py", line 135, in from_file
return m.from_file(filename)
File "C:\Users\FrancescoM\AppData\Local\Programs\Python\Python36-32\lib\site-packages\magic\magic.py", line 85, in from_file
with open(filename):
OSError: [Errno 22] Invalid argument: '*.txt'
But *.txt
is exactly what I want to input; I also would like to input whatever file *.*
Is this a problem related to python-magic
or caused by the way I input the files?
If you want to use things like * then you have to "glob" the files. This can be confusing because the globbing can happen in many places. I'm not familiar with the shell you are using, but if you were using bash then bash would perform the glob before it gets passed to Python. In other words, if *.txt actually matches something, bash replaces it with the list of files and then passes that list to Python (as separate arguments). It's then your job to deal with an arbitrary number of arguments in Python (probably using argparse). If *.txt doesn't match anything then it doesn't get expanded and Python sees *.txt and you have to handle that as an error.
Here it looks like the globbing isn't happening which either means there is nothing for your shell to match or your shell doesn't do globbing. If it doesn't do globbing then you can do the globbing in Python using the glob module. Usually globbing is something that is done by the shell, though.