I/O Operations .....

Discussion in 'Python' started by saif.shakeel@gmail.com, Apr 30, 2007.

  1. Guest

    Hi,
    I am parsing an XML file and sending the output to two files.The
    code asks the user to enter the input file,something like:

    file_input = raw_input("Enter The ODX File Path:")
    input_xml = open(file_input,'r')

    Now suppose the user enters the path as :
    C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.odx.xml

    I have 2 output files to which i have to redirect the output.The
    output file name should be same as input file in the same path ( the
    extension has to change to a format "ini" which is basically text file
    opened using notepad).Eg..
    output files should be :
    C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.ini, and,
    C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.xls

    Can someone help me in this.
    cheers
    , Apr 30, 2007
    #1
    1. Advertising

  2. > I am parsing an XML file and sending the output to two files.The
    > code asks the user to enter the input file,something like:
    >
    > file_input = raw_input("Enter The ODX File Path:")
    > input_xml = open(file_input,'r')
    >
    > Now suppose the user enters the path as :
    > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.odx.xml
    >
    > I have 2 output files to which i have to redirect the output.The
    > output file name should be same as input file in the same path ( the
    > extension has to change to a format "ini" which is basically text file
    > opened using notepad).Eg..
    > output files should be :
    > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.ini, and,
    > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.xls


    If you only would like to know how to write files, this might help:

    content1 = ..............
    content2 = ...............

    f = open( 'file1', 'w' )
    f.write( content1 )
    f.close( )

    f = open( 'file2', 'w' )
    f.write( content2 )
    f.close( )


    HTH,
    Daniel
    Daniel Nogradi, Apr 30, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 30, 2:13 pm, "Daniel Nogradi" <> wrote:
    > > I am parsing an XML file and sending the output to two files.The
    > > code asks the user to enter the input file,something like:

    >
    > > file_input = raw_input("Enter The ODX File Path:")
    > > input_xml = open(file_input,'r')

    >
    > > Now suppose the user enters the path as :
    > > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.odx.xml

    >
    > > I have 2 output files to which i have to redirect the output.The
    > > output file name should be same as input file in the same path ( the
    > > extension has to change to a format "ini" which is basically text file
    > > opened using notepad).Eg..
    > > output files should be :
    > > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.ini, and,
    > > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.xls

    >
    > If you only would like to know how to write files, this might help:
    >
    > content1 = ..............
    > content2 = ...............
    >
    > f = open( 'file1', 'w' )
    > f.write( content1 )
    > f.close( )
    >
    > f = open( 'file2', 'w' )
    > f.write( content2 )
    > f.close( )
    >
    > HTH,
    > Daniel- Hide quoted text -
    >
    > - Show quoted text -


    Hi,
    File writing can be done in that way,but my query is
    something different.I have to rename the output file by default with
    input file name(only changing the extension.
    Thanks
    , Apr 30, 2007
    #3
  4. In <>, saif.shakeel
    wrote:

    > File writing can be done in that way,but my query is
    > something different.I have to rename the output file by default with
    > input file name(only changing the extension.


    Take a look at the functions in `os.path`.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Apr 30, 2007
    #4
  5. > > > I am parsing an XML file and sending the output to two files.The
    > > > code asks the user to enter the input file,something like:

    > >
    > > > file_input = raw_input("Enter The ODX File Path:")
    > > > input_xml = open(file_input,'r')

    > >
    > > > Now suppose the user enters the path as :
    > > > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.odx.xml

    > >
    > > > I have 2 output files to which i have to redirect the output.The
    > > > output file name should be same as input file in the same path ( the
    > > > extension has to change to a format "ini" which is basically text file
    > > > opened using notepad).Eg..
    > > > output files should be :
    > > > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.ini, and,
    > > > C:\Projects\ODX Import\Sample Files\Global _A_UHP_Low_0.7.xls

    > >
    > > If you only would like to know how to write files, this might help:
    > >
    > > content1 = ..............
    > > content2 = ...............
    > >
    > > f = open( 'file1', 'w' )
    > > f.write( content1 )
    > > f.close( )
    > >
    > > f = open( 'file2', 'w' )
    > > f.write( content2 )
    > > f.close( )
    > >
    > > HTH,
    > > Daniel- Hide quoted text -
    > >
    > > - Show quoted text -

    >
    > Hi,
    > File writing can be done in that way,but my query is
    > something different.I have to rename the output file by default with
    > input file name(only changing the extension.



    Maybe something like this will help (on Linux, Windows is similar):

    >>> from os import path
    >>> f = '/tmp/hello.xls'
    >>> path.splitext( f )

    ('/tmp/hello', '.xls')
    >>> path.dirname( f )

    '/tmp'
    >>> path.basename( f )

    'hello.xls'


    Daniel
    Daniel Nogradi, Apr 30, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MtnSurf8

    Mathematical Operations in VHDL

    MtnSurf8, Apr 25, 2004, in forum: VHDL
    Replies:
    1
    Views:
    5,818
    Egbert Molenkamp
    Apr 25, 2004
  2. Divyang M
    Replies:
    3
    Views:
    587
    David Bishop
    Feb 4, 2005
  3. Liang

    array operations

    Liang, Dec 31, 2003, in forum: Perl
    Replies:
    2
    Views:
    2,186
    Liang
    Jan 5, 2004
  4. Jeremy Robbins

    Binary File Operations

    Jeremy Robbins, Jul 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    496
    Jeremy Robbins
    Jul 14, 2004
  5. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,021
    robert
    Feb 11, 2006
Loading...

Share This Page