understand program used to create file

P

pacopyc

Hi, I have about 10000 files .doc and I want know the program used to
create them: writer? word? abiword? else? I'd like develop a script
python to do this. Is there a module to do it? Can you help me?

Thanks
 
D

Dave Angel

Hi, I have about 10000 files .doc and I want know the program used to
create them: writer? word? abiword? else? I'd like develop a script
python to do this. Is there a module to do it? Can you help me?

Thanks
If you're on Linux, just use the process module to invoke "file" and
examine the results.

If you're not, then be more specific about the environment.
 
C

Chris Angelico

Hi, I have about 10000 files .doc and I want know the program used to
create them: writer? word? abiword? else? I'd like develop a script
python to do this. Is there a module to do it? Can you help me?

Technically, you can't find out just from the file what it was that
created it. But if you mean "figure out what type of file each one is"
(eg recognize an ODF, a PDF, a DOC, etc), then the easiest way is to
read in the first few bytes of the file and look for well-known magic
numbers[1]. As Dave says, Linux comes with a command that does exactly
that (and a bit more), called 'file'.

ChrisA
[1] http://en.wikipedia.org/wiki/Magic_number_(programming)
 
J

Jon Clements

Hi, I have about 10000 files .doc and I want know the program used to
create them: writer? word? abiword? else? I'd like develop a script
python to do this. Is there a module to do it? Can you help me?

Thanks

My suggestion would be the same as DaveA's.

This gives you the format it was *written* in.
(Saved a blank OO document as 95/97/XP Word DOC under Linux)

jon@forseti:~/filetest$ file *
saved-by-OO.doc: CDF V2 Document, Little Endian, Os: Windows, Version
1.0, Code page: -535, Author: jon , Revision Number: 0, Create Time/
Date: Mon Oct 31 20:47:30 2011

I'd be impressed if you could discover the program that did *write*
it; I'd imagine you'd need something that understood some meta-data in
the format (if the format has a kind of 'created_by' field, for
instance), or depend on nuisances which give away that a certain
program wrote data in another's native format.

Assuming the former, what might be possible:

1) Grab a "magic number" lookup list
2) Grab 8 (I think that should be all that's needed, but hey ummm..)
bytes from the start of each file
3) Look it up in the "magic number" list
4) If you got something great, if not compare 7, 6, 5, 4 bytes...
etc... until you get a hit or bail out

(Or just find a Windows port of 'file')

HTH

Jon.
 
A

alex23

Hi, I have about 10000 files .doc and I want know the program used to
create them: writer? word? abiword? else? I'd like develop a script
python to do this. Is there a module to do it? Can you help me?

Word documents store metadata inside of them, one field of which is
the program used to create them. This shows you how to use pywin32 to
access them:

http://www.galalaly.me/index.php/20...crosoft-word-documents-using-pywin32-library/

This won't be a foolproof solution, unfortunately. A random
examination of doc files shows that not all of them have the required
field set.
 
V

vonkes

Ho trovato questo script in una directory dove sono conservati molti
demo per python non era funzionante,sono riuscito a capirne le
finalità e così l'ho reso funzionante.
Esamina un file di testo e analizza il carattere di fine riga. le
possibilità dipendono dal sistema operativo usato. Nei file di testo
sotto S.O. Unix il carattere di fine riga in genere è un CR o carriage
return. Sotto Mac questo carattere in genere è un LF linee feed
mentre sotto s.o. windows in genere è CRLF sulla base di ciò
l'applicativo osserva tutti i fine riga e ne fa un confronto.
Osservando il sorgente si dovrebbe comprendere come il controllo viene
fatto.
Nell'ipotesi che ci si voglia divertire e non si dia un nome di file o
si dia un nome inesistente, l'applicativo fa il controllo su se stesso
e poi da le risposte.
Provate:
ecco il sorgente:
__________ inizio applicativo______________
!/usr/bin/python
# -*- coding: utf-8 -*-
"""Controlla file di testo -
Controlla quale sia il carattere di fine riga per determinarne lo
stile
Se la riga termina con CR abbiamo lo stile UNIX
Se " " " " LF abbiamo lo stile MAC
Se " " " " CRLF abbiamo lo stile Win/MS-Dos
Mettiamo una alternativa all'inserimento del file aggiungiamo
una parte di codice che nel caso non venga indicato un nome
di file o ne indicassimo uno mancante o assente, vada a cercare il
file
dal quale siamo partiti cioe' questo stesso che dovrebbe essere
contenuto
nella variabile argv che come e' noto e' una lista di parametri
passati dalla
linea di comando e il primo elemento della lista e' proprio il
nome di questo
applicativo che seppure non è il classico file di testo anch'esso
ha i caratteri
di fine riga esattamente come quelli dei classici file di testo e
quindi possiamo
esaminarli per decidere quale stile seguono.
"""
#---------- fine commento-------------
import sys,os

import string
def Message(parametro):
#print "\nquesta e' la funzione che stampera' Il parametro ricevuto"
print "\n:",parametro

def ChiediFiledaAprire(message):
print "\n"+message
nome = raw_input( "\ninserisci il nome di un file di testo completo
di estensione \n: ")
return nome

#pathname = sys.path
#print "pathname=",pathname
def main():
#print os.listdir(sys.path[0])
while True:
name = ChiediFiledaAprire(message='Per controllo del carattere di
fine linea presente nel file')
if name == "":
print "Poiche non hai digitato alcun nome il controllo lo
effettuer??\
il controllo sul file "+ sys.argv[0]
name = sys.argv[0];break
elif not name:
print "Attenzione !! Il nome file digitato non esiste, effettuer??
controllo sul file "+sys.argv[0] +" che ?n esecuzione "
name = sys.argv[0];break
else:
break
fp = open(name, 'rb')

try:
data = fp.read()
#print "nel file",name," abbiamo trovato",data
except MemoryError:
Message("Attenzione, Il file e' troppo grande.")
sys.exit(0)
if len(data) == 0:
Message('Il File e vuoto.')
sys.exit(0)
number_cr = string.count(data, '\r')
number_lf = string.count(data, '\n')
if number_cr == number_lf == 0:
Message('Nel File '+name+' manca il carattere di fine riga.')
if number_cr != 0 and chr(10) not in data:
Message('Il File '+name+' presenta lo style unix con fine riga =
CR')
elif number_lf != 0 and chr(13) not in data:
Message('Il File '+name+' presenta lo style mac con fine riga = LF')
elif number_cr == number_lf:
Message('Il File '+name+' presenta lo style Win/MS-Dos con fine riga
=CRLF')
else:
Message('Il File '+name+' presenta un fine riga non riconosciuto
(binary file?)')
sys.exit(0)

#----------------fine applicativo----------
Ciao vonkes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top