473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Re: check if file is MS Word or PDF file

On Sat, Sep 27, 2008 at 3:42 PM, Michael Crute <mc****@gmail.comwrote:
On Sat, Sep 27, 2008 at 5:43 PM, A. Joseph <jo******@gmail.comwrote:
>What should I look for in a file to determine whether or not it is a
MS Word file or an Excel file or a PDF file, etc., etc.? including Zip
files

I don`t want to check for file extension.
os.path.splitext('Filename.jpg') will produce a tuple of filename and
extension, but some file don`t even have extension and can still be read by
MS Word or NotePad. i want to be 100% sure of the file.

You could use the mimetypes module...

<<< import mimetypes
<<< mimetypes.guess_type("LegalNotices.pdf")
>>>('application/pdf', None)
Looking at the docs for the mimetypes module, it just guesses based on
the filename (and extension), not the actual contents of the file, so
it doesn't really help the OP, who wants to make sure their program
isn't misled by an inaccurate extension.

Regards,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
-mike

--
________________________________
Michael E. Crute
http://mike.crute.org

God put me on this earth to accomplish a certain number of things.
Right now I am so far behind that I will never die. --Bill Watterson
--
http://mail.python.org/mailman/listinfo/python-list
Sep 27 '08 #1
1 3830
On Sep 27, 4:01*pm, "Chris Rebert" <c...@rebertia.comwrote:
On Sat, Sep 27, 2008 at 3:42 PM, Michael Crute <mcr...@gmail.comwrote:
On Sat, Sep 27, 2008 at 5:43 PM, A. Joseph <joefa...@gmail.comwrote:
What should I look for in a file to determine whether or not it is a
MS Word file or an Excel file or a PDF file, etc., etc.? including Zip
files
I don`t want to check for file extension.
os.path.splitext('Filename.jpg') will produce a tuple of filename and
extension, but some file don`t even have extension and can still be read by
MS Word or NotePad. i want to be 100% sure of the file.
You could use the mimetypes module...
<<< import mimetypes
<<< mimetypes.guess_type("LegalNotices.pdf")
>>('application/pdf', None)

Looking at the docs for the mimetypes module, it just guesses based on
the filename (and extension), not the actual contents of the file, so
it doesn't really help the OP, who wants to make sure their program
isn't misled by an inaccurate extension.

Regards,
Chris
--
Follow the path of the Iguana...http://rebertia.com
-mike
--
________________________________
Michael E. Crute
http://mike.crute.org
God put me on this earth to accomplish a certain number of things.
Right now I am so far behind that I will never die. --Bill Watterson
--
http://mail.python.org/mailman/listinfo/python-list
Check http://sourceforge.net/project/showf...group_id=23617

for the 'file' command for Windows.

~Sean
Sep 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CR | last post by:
having a problem with the final strcpy of filename from *argv into a structure containing an array with 20 elements called fname It seems that strcpy & strncpy aren't stopping after null is found...
4
by: giftson.john | last post by:
Hi, I am creating an application which migrates all documents from one repository to another repository. Before migration i have to verify all the documents are unique. No duplicates has to be...
0
by: Chris Rebert | last post by:
On Sat, Sep 27, 2008 at 2:43 PM, A. Joseph <joefazee@gmail.comwrote: Well, if you're on unix, you could run the 'file' command and check its output using the 'subprocess' module, but it sounds...
0
by: Michael Crute | last post by:
On Sat, Sep 27, 2008 at 5:43 PM, A. Joseph <joefazee@gmail.comwrote: You could use the mimetypes module... <<< import mimetypes <<< mimetypes.guess_type("LegalNotices.pdf") -mike --
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.