473,404 Members | 2,179 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,404 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 3834
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.