How to execute a python script and write output to txt file?

后端 未结 8 592
离开以前
离开以前 2020-12-10 14:31

I\'m executing a .py file, which spits out a give string. This command works fine

execfile (\'file.py\')

But I want the output (in addition to it being shown

相关标签:
8条回答
  • 2020-12-10 15:03

    You could also do this by going to the path of the folder you have the python script saved at with cmd, then do the name.py > filename.txt It worked for me on windows 10

    0 讨论(0)
  • 2020-12-10 15:06

    This'll also work, due to directing standard out to the file output.txt before executing "file.py":

    import sys
    
    orig = sys.stdout
    with open("output.txt", "wb") as f:
        sys.stdout = f
        try:
            execfile("file.py", {})
        finally:
            sys.stdout = orig
    

    Alternatively, execute the script in a subprocess:

    import subprocess
    
    with open("output.txt", "wb") as f:
        subprocess.check_call(["python", "file.py"], stdout=f)
    

    If you want to write to a directory, assuming you wish to hardcode the directory path:

    import sys
    import os.path
    
    orig = sys.stdout
    with open(os.path.join("dir", "output.txt"), "wb") as f:
        sys.stdout = f
        try:
            execfile("file.py", {})
        finally:
            sys.stdout = orig
    
    0 讨论(0)
  • 2020-12-10 15:11

    If you are running the file on Windows command prompt:

    python filename.py >> textfile.txt
    

    The output would be redirected to the textfile.txt in the same folder where the filename.py file is stored.

    The above is only if you have the results showing on cmd and you want to see the entire result without it being truncated.

    0 讨论(0)
  • 2020-12-10 15:12

    using some hints from Remolten in the above posts and some other links I have written the following:

    from os import listdir
    from os.path import isfile, join
    folderpath = "/Users/nupadhy/Downloads"
    filenames = [A for A in listdir(folderpath) if isfile(join(folderpath,A))]
    newlistfiles = ("\n".join(filenames))
    OuttxtFile = open('listallfiles.txt', 'w')
    OuttxtFile.write(newlistfiles)
    OuttxtFile.close()
    

    The code above is to list all files in my download folder. It saves the output to the output to listallfiles.txt. If the file is not there it will create and replace it with a new every time to run this code. Only thing you need to be mindful of is that it will create the output file in the folder where your py script is saved. See how you go, hope it helps.

    0 讨论(0)
  • 2020-12-10 15:16

    what your doing is checking the output of execfile('file.py') against the string 'output.txt'

    you can do what you want to do with subprocess

    #!/usr/bin/env python
    import subprocess
    with open("output.txt", "w+") as output:
        subprocess.call(["python", "./script.py"], stdout=output);
    
    0 讨论(0)
  • 2020-12-10 15:18
    file_open = open("test1.txt", "r")
    file_output = open("output.txt", "w")
    
    for line in file_open:
        print ("%s"%(line), file=file_output)
    
    file_open.close()
    file_output.close()
    
    0 讨论(0)
提交回复
热议问题