How to add an extension to an input filename?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runsun
    New Member
    • May 2007
    • 17

    How to add an extension to an input filename?

    Read from an existing file, then output a file with the same name but an extension:

    file = raw_input ('Enter the filename:\n')
    fileout = open ('file.out', 'w')

    However, when I enter a filename like "test", it gives the output file as "file.out" instead of "test.out".
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by runsun
    Read from an existing file, then output a file with the same name but an extension:

    file = raw_input ('Enter the filename:\n')
    fileout = open ('file.out', 'w')

    However, when I enter a filename like "test", it gives the output file as "file.out" instead of "test.out".
    Don't use file as variable name.
    you wanted a .out extension for the output filename that the user types. therefore you should declare your filename as:
    Code:
    filename = raw_inpu("blah....")
    filename = filename + ".out")
    fileout = open(filename,"w")
    ....

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by runsun
      Read from an existing file, then output a file with the same name but an extension:

      file = raw_input ('Enter the filename:\n')
      fileout = open ('file.out', 'w')

      However, when I enter a filename like "test", it gives the output file as "file.out" instead of "test.out".
      Ok. Here's what you want to do: Open the file that has your input in "r"ead mode. Read the data. Close the input file. Do some work on the data. Open a new file for output in "w"rite mode. write the data. Close the output file. Basically:
      [CODE=python]
      inputFileName = raw_input ('Enter the filename:\n')
      outputFileName = inputFileName + ".out"

      inFile = open(inputFileN ame, 'r')
      dataAsList = inFile.readline s()
      inFile.close()

      # simulate some work here
      for line in dataAsList:
      print line

      outFile = open (outputFileName , 'w')
      outFile.writeli nes(dataAsList)
      outFile.close()
      [/CODE]
      Hope that helps.

      Comment

      • runsun
        New Member
        • May 2007
        • 17

        #4
        That works well. Thank ghostdog74!

        Originally posted by ghostdog74
        Don't use file as variable name.
        you wanted a .out extension for the output filename that the user types. therefore you should declare your filename as:
        Code:
        filename = raw_inpu("blah....")
        filename = filename + ".out")
        fileout = open(filename,"w")
        ....

        Comment

        • runsun
          New Member
          • May 2007
          • 17

          #5
          Thank you very much!
          Not only did you answer this extension question, but also answered those related to my previous questions!

          Originally posted by bartonc
          Ok. Here's what you want to do: Open the file that has your input in "r"ead mode. Read the data. Close the input file. Do some work on the data. Open a new file for output in "w"rite mode. write the data. Close the output file. Basically:
          [CODE=python]
          inputFileName = raw_input ('Enter the filename:\n')
          outputFileName = inputFileName + ".out"

          inFile = open(inputFileN ame, 'r')
          dataAsList = inFile.readline s()
          inFile.close()

          # simulate some work here
          for line in dataAsList:
          print line

          outFile = open (outputFileName , 'w')
          outFile.writeli nes(dataAsList)
          outFile.close()
          [/CODE]
          Hope that helps.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by runsun
            Thank you very much!
            Not only did you answer this extension question, but also answered those related to my previous questions!
            You are welcome, very much.
            Thank you for joining TheScripts.com.
            Keep posting,
            Barton

            Comment

            Working...