determining file type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Raj Sarraf

    #1

    determining file type

    Hi,

    I have a funtion named unzipper() which does the work of unzipping the
    files.

    Is there a way I can identify what is the type of the file which'll be
    passed to unzipper().
    If yes, I'll be able to make out if it's a zip file or a tar or a bz2
    file.

    Thanks,
    Ritesh

  • Maric Michaud

    #2
    Re: determining file type

    Le Mercredi 14 Juin 2006 11:22, Ritesh Raj Sarraf a écrit :[color=blue]
    > Hi,
    >
    > I have a funtion named unzipper() which does the work of unzipping the
    > files.
    >
    > Is there a way I can identify what is the type of the file which'll be
    > passed to unzipper().
    > If yes, I'll be able to make out if it's a zip file or a tar or a bz2
    > file.
    >[/color]
    Under Linux you can use the file command in a pipe.
    In all cases, you use the "magic" repository of the file command directly like
    this (I found the '\xff\xd8' for jpeg in /usr/share/file/magic.mime on my
    debian)

    In [69]: f = file ('samurai_tux.j pg')

    In [70]: s = f.read(2)

    In [71]: s == '\xff\xd8'
    Out[71]: True



    [color=blue]
    > Thanks,
    > Ritesh[/color]

    --
    _____________

    Maric Michaud
    _____________

    Aristote - www.aristote.info
    3 place des tapis
    69004 Lyon
    Tel: +33 426 880 097

    Comment

    • Ritesh Raj Sarraf

      #3
      Re: determining file type

      But isn't there any library function ?
      Something like
      XX.filetype(nam e)
      Directory
      File-Tar
      File-Zip
      File-MPEG

      Ritesh


      Maric Michaud wrote:[color=blue]
      > Le Mercredi 14 Juin 2006 11:22, Ritesh Raj Sarraf a écrit :[color=green]
      > > Hi,
      > >
      > > I have a funtion named unzipper() which does the work of unzipping the
      > > files.
      > >
      > > Is there a way I can identify what is the type of the file which'll be
      > > passed to unzipper().
      > > If yes, I'll be able to make out if it's a zip file or a tar or a bz2
      > > file.
      > >[/color]
      > Under Linux you can use the file command in a pipe.
      > In all cases, you use the "magic" repository of the file command directlylike
      > this (I found the '\xff\xd8' for jpeg in /usr/share/file/magic.mime on my
      > debian)
      >
      > In [69]: f = file ('samurai_tux.j pg')
      >
      > In [70]: s = f.read(2)
      >
      > In [71]: s == '\xff\xd8'
      > Out[71]: True
      >
      >
      >
      >[color=green]
      > > Thanks,
      > > Ritesh[/color]
      >
      > --
      > _____________
      >
      > Maric Michaud
      > _____________
      >
      > Aristote - www.aristote.info
      > 3 place des tapis
      > 69004 Lyon
      > Tel: +33 426 880 097[/color]

      Comment

      • Ritesh Raj Sarraf

        #4
        Re: determining file type

        Also,
        f = file ('some_file.jpg ')

        throws an error.
        "str object is not callable"

        Ritesh

        Maric Michaud wrote:[color=blue]
        > Le Mercredi 14 Juin 2006 11:22, Ritesh Raj Sarraf a écrit :[color=green]
        > > Hi,
        > >
        > > I have a funtion named unzipper() which does the work of unzipping the
        > > files.
        > >
        > > Is there a way I can identify what is the type of the file which'll be
        > > passed to unzipper().
        > > If yes, I'll be able to make out if it's a zip file or a tar or a bz2
        > > file.
        > >[/color]
        > Under Linux you can use the file command in a pipe.
        > In all cases, you use the "magic" repository of the file command directlylike
        > this (I found the '\xff\xd8' for jpeg in /usr/share/file/magic.mime on my
        > debian)
        >
        > In [69]: f = file ('samurai_tux.j pg')
        >
        > In [70]: s = f.read(2)
        >
        > In [71]: s == '\xff\xd8'
        > Out[71]: True
        >
        >
        >
        >[color=green]
        > > Thanks,
        > > Ritesh[/color]
        >
        > --
        > _____________
        >
        > Maric Michaud
        > _____________
        >
        > Aristote - www.aristote.info
        > 3 place des tapis
        > 69004 Lyon
        > Tel: +33 426 880 097[/color]

        Comment

        • Diez B. Roggisch

          #5
          Re: determining file type

          Ritesh Raj Sarraf schrieb:[color=blue]
          > Also,
          > f = file ('some_file.jpg ')
          >
          > throws an error.
          > "str object is not callable"[/color]

          I bet you have rebound "file" to a string, like this:

          file = "some name"


          And PLEASE don't top-quote:




          Diez

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: determining file type

            In <1150281704.342 529.206640@u72g 2000cwu.googleg roups.com>, Ritesh Raj
            Sarraf wrote:
            [color=blue]
            > Also,
            > f = file ('some_file.jpg ')
            >
            > throws an error.
            > "str object is not callable"[/color]

            This happens if you rebind names of builtins. In this case to a string.
            Don't do that!

            In [5]:file
            Out[5]:<type 'file'>

            In [6]:file = 'foo.txt'

            In [7]:file
            Out[7]:'foo.txt'

            In [8]:file('x')
            ---------------------------------------------------------------------------
            exceptions.Type Error Traceback (most recent call last)

            /home/bj/<ipython console>

            TypeError: 'str' object is not callable


            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • Maric Michaud

              #7
              Re: determining file type

              Le Mercredi 14 Juin 2006 12:41, Ritesh Raj Sarraf a écrit :[color=blue]
              > Also,
              > f = file ('some_file.jpg ')
              >
              > throws an error.
              > "str object is not callable"[/color]
              stange, did you define a function named file ?
              You can use open instead.

              --
              _____________

              Maric Michaud
              _____________

              Aristote - www.aristote.info
              3 place des tapis
              69004 Lyon
              Tel: +33 426 880 097

              Comment

              • Sion Arrowsmith

                #8
                Re: determining file type

                Ritesh Raj Sarraf <riteshsarraf@g mail.com> wrote:[color=blue]
                >Also,
                >f =3D file ('some_file.jpg ')
                >
                >throws an error.
                >"str object is not callable"[/color]

                You know all the times people say in this group "Don't use list
                or str or file[HINT] or anything else that shadows a built in as
                a variable name"? Now you know why.

                --
                \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
                ___ | "Frankly I have no feelings towards penguins one way or the other"
                \X/ | -- Arthur C. Clarke
                her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

                Comment

                • utabintarbo

                  #9
                  Re: determining file type




                  Comment

                  Working...