如何从文本文件重命名 python 中的文件
How to rename Files in python from a text file
我正在进行一个在特定时间修剪视频,然后将其转换为音频文件并保存的项目。
但问题是我无法获取存储在 .txt 文件中的每个新创建的具有特定名称的音频文件
这是代码
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")
我试过同时使用 shutil.move
和 os.rename
,但都产生相同的错误
这是输出:
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:for 循环的缩进错误。
2:您应该从列表中的每个文件名中删除代表换行符的“\n”:
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")
我正在进行一个在特定时间修剪视频,然后将其转换为音频文件并保存的项目。 但问题是我无法获取存储在 .txt 文件中的每个新创建的具有特定名称的音频文件
这是代码
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")
我试过同时使用 shutil.move
和 os.rename
,但都产生相同的错误
这是输出:
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:for 循环的缩进错误。
2:您应该从列表中的每个文件名中删除代表换行符的“\n”:
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")