Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add stdout to text file
#1
Alright, I've tried a great many thing to try to get this to work. This iteration is the closest I've gotten to success. What I'm trying to do is write two lines, and only two lines, to a text file and then create a new file. The format is as such:

file1.txt
file 'C:\path\to\audiofile1.wav'
file 'C:\path\to\audiofile2.wav'

I'm using ffprobe to analyze an undetermined amount of audio tracks and essentially sort them by track length. I was at first trying to write the output ffprobe straight to the text file, but every out put was overwriting the first line and there would only be one file with one line, the last track in the directory. So then I tried to send the output of ffprobe to an array and print that to a text file. That works a little better, but either I only write 1 line to 1 file, or the entire array to only 1 file. Here's my code. I'm exhausted right now, I'll answer any questions and supply more code if you need. You'll see some bits commented out. I tend to leave those in there while I think about what I might change.

clipOrig = "D:\\path\\to\\audio\\files"
os.chdir(clipOrig)

current_files = glob.glob("sFile*.txt")
file_count = 1
file = open(f"sFile{file_count}.txt", "w")

while f"sFile{file_count}.txt" in current_files:
    file_count += 1
arr = []
def write_file():
    global file_count, count
    count = sum(1 for line in open(f"sFile{file_count}.txt"))
    # count = 1
    if(count == 2):
        file_count +=1
        count = 1
    else:
        for i in arr:
            print("file ", f"'{i}'", "\n", file=text_file)
            count += 1

for f in os.listdir():
    ffprobe = subprocess.check_output(['ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', '-of', 'csv=p=0', '-i', f], stderr=subprocess.PIPE, stdin=subprocess.PIPE).decode('utf-8').rstrip()
    with open(f"sFile{file_count}.txt", "w") as text_file:
        if float(ffprobe) < 35:
            ffilename = subprocess.check_output(['ffprobe', '-v', 'quiet', '-show_entries', 'format=filename', '-of', 'csv=p=0', '-i', f]).decode('utf-8').rstrip()
            arr.append(f"{clipOrig}\\{ffilename}")
        else:
            print("false")

write_file()
Reply


Messages In This Thread
Add stdout to text file - by maxtimbo - Jan-28-2019, 03:09 AM
RE: Add stdout to text file - by ichabod801 - Jan-28-2019, 03:57 AM
RE: Add stdout to text file - by maxtimbo - Jan-28-2019, 02:45 PM
RE: Add stdout to text file - by maxtimbo - Feb-05-2019, 12:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [subprocess] Why stdout sent to stderr? Winfried 3 521 Jan-26-2024, 07:26 PM
Last Post: snippsat
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,155 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Performance options for sys.stdout.writelines dgrunwal 11 3,195 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,733 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,060 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,411 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  changing stdout and stderr Skaperen 4 2,721 Dec-02-2020, 08:58 PM
Last Post: Skaperen
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,463 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,428 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Get stdout of a running process yok0 0 3,055 Aug-20-2020, 10:12 AM
Last Post: yok0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020