I have a list that includes the paths of the files (in my case movies), which is called collectMovies
and has followed data structure:
['C:\\Users\\UserName\\Desktop\\Movies\\pso-cash-sd.mkv',
'C:\\Users\\UserName\\Desktop\\Movies\\hda-casino.royale.uncut-1080p.mkv']
How can I compress the movies, so that only the movie and not the whole directories are compressed and shown up in the 7zip archive?
What I tried was that:
import py7zr
with py7zr.SevenZipFile("files.7z","w") as mov:
for val in collectMovies:
val = val.replace(r"\\",r"\\")
try:
mov.write(val)
except:
print("File not Found")
This works, but it writes also the directories. I only want to write the movie file in the archive.
C:\\Users\\UserName\\Desktop\\Movies\\pso-cash-sd.mkv
What I want is, that only pso-cash-sd.mkv
is shown up in the compressed archive.
The actual output looks like this (with subfolders):
What I want is, that only the movie(s) are shown up, like:
Option 1: Eliminate building a list of images from the Movies directory and having to manipulate the path string by using os.listdir()
and os.chdir()
. IMHO this is a cleaner and less error prone method, should you want to change the path to your movie files.
import os
import py7zr
movie_dir = r'"C:\Users\UserName\Desktop\Movies\"'
with py7zr.SevenZipFile('files.7z','w') as mov:
for item in os.listdir(movie_dir):
try:
os.chdir(movie_dir)
mov.write(item)
except:
print('File not Found')
Option 2: Use the list method used in your original question, extract the movie filename from the path string and use os.chdir()
.
import os
import py7zr
movie_dir = r'"C:\Users\UserName\Desktop\Movies\"'
collectMovies = ['C:\\Users\\UserName\\Desktop\\Movies\\pso-cash-sd.mkv',
'C:\\Users\\UserName\\Desktop\\Movies\\hda-casino.royale.uncut-1080p.mkv']
with py7zr.SevenZipFile('files.7z','w') as mov:
for val in collectMovies:
# extract file name from path:
val = val.split('\\')[5]
try:
os.chdir(movie_dir)
mov.write(val)
except:
print('File not Found')
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.