简体   繁体   中英

How to rename Files in python from a text file

I am on a project that trims video at specific time,then converts it to an audio file and save it. But the problem is i cannot get every Newly created audio file with a specific name that is stored inside a.txt file

Here's the Code

from moviepy.editor import *
import shutil

s= open("begin.txt")   #Starting time
lines1 = s.readlines()

e = open("end.txt")    #End Time
lines2 = e.readlines()

t = open("title.txt")  #The text file  which contain the file name
lines3 = t.readlines()


print("Starting...")


for x in range(1776): #Iam triming the video files to 1777 parts
  st=lines1[x]
  en=lines2[x]
  t=lines3[x]


 print("Start Time: "+st)
 print("End Time: "+en)

 video = VideoFileClip("J:\Movie\SM.mp4").subclip(st, en)
 video.audio.write_audiofile("J:\Movie\ audio.mp3")
 shutil.move("J:\Movie\ audio.mp3","J:\Movie\Data\ "+t+".mp3")

I have tried using both shutil.move and os.rename but both produce the same Error

Here's the Output:

Starting...
Start Time: 39.33

End Time: 41.958

MoviePy - Writing audio in J:\Movie\ audio.mp3
Traceback (most recent call last):
File "C:\Users\CRPSM\Anaconda3\lib\shutil.py", line 563, in move
os.rename(src, real_dst)
OSError: [WinError 123] The filename, directory name, or volume label syntax 
is incorrect: 'J:\\Marvel\\ audio.mp3' -> 'J:\\Marvel\\Data\\ Things are 
never 
gonna bethe same now\n.mp3'

 During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/CRPSM/PycharmProjects/AC#/venv/fdsh.py", line 27, in <module>
shutil.move("J:\Marvel\ audio.mp3","J:\Marvel\Data\ "+t+".mp3")
File "C:\Users\CRPSM\Anaconda3\lib\shutil.py", line 577, in move
copy_function(src, real_dst)
File "C:\Users\CRPSM\Anaconda3\lib\shutil.py", line 263, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
 File "C:\Users\CRPSM\Anaconda3\lib\shutil.py", line 121, in copyfile
with open(dst, 'wb') as fdst:
OSError: [Errno 22] Invalid argument: 'J:\\Marvel\\Data\\ Things are never 
gonna bethe same now\n.mp3'
MoviePy - Done.

Process finished with exit code 1

1: You have an indentation error for the for-loop.

2: You should remove the '\n', which stands for newline, from each file name in you list:

from moviepy.editor import *
import shutil

s= open("begin.txt")   #Starting time
lines1 = [l.strip() for l in s.readlines()]

e = open("end.txt")    #End Time
lines2 = [l.strip() for l in e.readlines()]

t = open("title.txt")  #The text file  which contain the file name
lines3 = [l.strip() for l in t.readlines()]


print("Starting...")


for x in range(1776): #Iam triming the video files to 1777 parts
     st=lines1[x]
     en=lines2[x]
     t=lines3[x]

     print("Start Time: "+st)
     print("End Time: "+en)

     video = VideoFileClip("J:\Movie\SM.mp4").subclip(st, en)
     video.audio.write_audiofile("J:\Movie\ audio.mp3")
     shutil.move("J:\Movie\ audio.mp3","J:\Movie\Data\ "+t+".mp3")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM