I have a folder full of files and these doesn't have an extension. How can I check file types? I want to check the file type and change the filename accordingly. Let's assume a function filetype(x)
returns file type like png
. I want to do this:
files = os.listdir(".")
for f in files:
os.rename(f, f+filetype(f))
How do I do this?
On unix and linux there is the
file
command to guess file types. There's even a windows port.From the man page:
You would need to run the
file
command with thesubprocess
module and then parse the results to figure out an extension.edit: Ignore my answer. Use Chris Johnson's answer instead.