1

I'm attempting to write a python script that will execute in the current directory, iterate over all files, and then rename each file with a prefixed date (returned from the utility mediainfo and the tagged date of the video). As a result, each video would be renamed with a prefixed date based on the tagged date. (I'll probably use that info later to make symbolic links of each video by year in a folder). Below is my code. It currently renames the files with a 0 and prints out the date. I dont care what it prints out. I want it to rename it with the date. Any help is appreciated.

#!/usr/bin/env python

import os
import shutil
import re

def rename():
    dirname = os.getcwd()
    for item in os.listdir(dirname):
        if os.path.isfile(os.path.join(dirname,item)):
            if item.startswith('20'):
                print ("Skipping File: %s" % (item))
            else:
                timeformat = os.system('mediainfo ' + item + " |grep -i tagged|tail -1|awk -F: '{print $2}'|awk '{print $2}'|tail -1")
                timeformat = re.sub('0$','', str(timeformat))
                newfile = str(timeformat) + str(item)
                shutil.move(item, newfile)


def main():
    rename()

main()
user192085
  • 117
  • 1
  • 1
  • 11
  • it might print the date because thats the output of the os.system call. Also as timeformat is "0" after your os.system call, the script seems to work as designed. Please check the contents of variable str(timeformat) before and after your regex. Actually you can delete the regex. The solution for your problem is that you need to catch STDOUT of your system call. please check https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on – Harry Oct 14 '18 at 18:18
  • Possible duplicate of [Assign output of os.system to a variable and prevent it from being displayed on the screen](https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on) – Harry Oct 14 '18 at 18:24

0 Answers0