Output strings to directory locally

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flawrence
    New Member
    • Sep 2014
    • 3

    Output strings to directory locally

    Is it possible to output strings created from a print statement into a directory locally?
    If so, would you use sysstdout module?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can assign sys.stdout to an open file object.
    Code:
    f = open("test.txt", 'w')
    sys.stdout = f
    Subsequent print statements will direct the output to the open file.

    Comment

    • flawrence
      New Member
      • Sep 2014
      • 3

      #3
      Thank you! However, my files do not have a .etc extension. They are unique and I need them to actually be propagated straight into a directory. Is this possible?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        It's a good idea to save the old value of sys.stdout and restore it when you are done.
        Code:
        stdout_old = sys.stdout
        sys.stdout = open("test.txt", 'a')
        # do stuff
        sys.stdout = stdout_old

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Not that I know of. Information is stored on disc in files. A directory is not a file. BTW, File objects don't have to have extensions.

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            If you are running linux you can pipe standard output to a file directly

            program_name.py > output_print_fi le.txt

            Comment

            • flawrence
              New Member
              • Sep 2014
              • 3

              #7
              So I tried the sysstd output with this code
              Code:
              import os
              
              path = '/Users/felishalawrence/testswps/vol1'  
              for file in os.listdir(path):
                      newFile = file[:file.rindex("v")]+"v20"
              sys.stdout =  '/Users/felishalawrence/testswps/vol1'
              print newFile
              and got nothing. No errors and it did not print out my results in ipython notebook when I removed line 5 and ran it again. The output was also not in the folder specified. What did I do wrong? Can I reset it? If so, how?
              Last edited by bvdet; Sep 15 '14, 09:42 AM. Reason: Add code tags. Please use code tags when posting code. [code].....[/code]

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                In your for loop, you define a value to identifier "newfile", but you perform no action with this value. After the for loop, you assign sys.stdout to a directory when it should be assigned to an open file object. The following print statement goes nowhere.

                Comment

                Working...