473,395 Members | 1,411 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,395 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 3833
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: 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
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:
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.