0
我不是程序員,但想在Linux中使用Windows python腳本。它對我認爲的文件名有困難。可能是因爲需要成功,但這個腳本中有很多,我不知道他們做了什麼。希望有人不會介意看看它,也許別人會發現它有用。它需要一個csv文件並使用它來創建用於媒體服務器的正確文件名。例如,我在一個文件夾中有一系列的電視節目,這將使用csv文件中的正確名稱匹配文件並重命名文件。需要幫助將小python腳本從windows轉換爲linux
這就是:
# Episode Name - File Renamer
# Renames files without accurate episode order using the Episode Name only
# Coded by: Tim.
# Import modules
import os
import glob
import csv
# Assign inital values
repeat = "true"
edit = "true"
#Define custom functions
def invalid_char(s):
"""
Strip the invalid filename characters from the string selected.
Feel free to add/remove additional .replace(X,X) as needed if you
want to remove other characters even if they are valid.
For example: , or [ or !
"""
return s.replace("?","").replace(":","").replace("*","").replace("<","").replace(">","").replace("|","").replace("/","").replace("\\","").replace('"',"")
def season(l):
"""
Takes the first cell of the CSV copied from the TVDB website
and strips out only the season.
"""
if l == "Special":
season = "00"
else:
season = l.split(" ")[0].zfill(2)
return season
def episode(l):
"""
Takes the first cell of the CSV copied from the TVDB website
and strips out only the episode. Pads a 0 before single digits.
"""
if l == "Special":
episode = "00"
else:
episode = l.split(" ")[-1].zfill(2)
return episode
# Overall loop, allows user to re-run the entire script
while repeat == "true":
# Checks if the user defined variables need to be edited
if edit == "true":
# Prompt user to define static variables
series_name = raw_input("Please enter your series name: ")
#series_name = "Charlie Brown"
print "\n"
data = raw_input("Path to CSV: ")
#data = "C:\lt\cb.csv"
print "\n"
dir1 = raw_input("Path to episodes (format C:\*): ")
#dir1 = "M:\TV Shows\CB\all\*"
print "\n"
move = raw_input("Would you like to move renamed files? (Yes/No): ").lower()
if move in ("y", "ye", "yes"):
print "\n"
print "Enter path to root folder where files should be moved"
move_path = raw_input("and season folders will be created (format C:\Show\): ")
edit = "false"
file_list = glob.glob(dir1)
print ("\n\n")
# Loop through file_list and look for matches in the CSV to the filename after the prefix assigned
for file in file_list:
fname = file
ext = fname[-4:]
with open(data, 'r') as file:
reader = csv.reader(file)
season_episode_name = ["S" + season(line[0]) + "E" + episode(line[0]) + " " + invalid_char(line[1]) for line in reader if invalid_char(line[1].lower()) in fname.lower() and line[1].lower() != ""]
season_dir = (''.join(season_episode_name)).split("E")[0][1:]
if season_episode_name:
season_episode_name = ''.join(season_episode_name)
fname2 = dir1[:-1] + series_name + " " + season_episode_name + ext
# If user chose to move files to another directory, then fname2 has the path replaced
if move in ("y", "ye", "yes"):
fname2 = move_path + "Season " + season_dir + "\\" + fname2[len(dir1[:-1]):]
# Generates the season directory if does not already exist
if not os.path.exists(move_path + "Season " + season_dir):
os.makedirs(move_path + "Season " + season_dir)
# Rename file and move if user requested
print fname + "\n >>> " + fname2 + "\n"
os.rename(fname, fname2)
else:
print fname + "\n >>> " "Unable to locate match, please rename manually.\n"
# Check if user wants to repeat, edit or exit
repeat = raw_input ("\n\nType R to run again, Type E to edit the parameters, or Q to quit: ")
if repeat.lower() in ("r", "retry"):
repeat = "true"
print "\nRunning again with the same parameters.."
elif repeat.lower() in ("e", "edit"):
edit = "true"
repeat = "true"
print "\nEditing paramaters before running again.."
elif repeat.lower() in ("q", "quit"):
repeat = "false"
print "\nQuitting..."
else:
repeat = "false"
print "\nInvalid command."
# When repeat no longer is "true" the script exiits with the below message
else:
raw_input("\n\nPress enter to exit...")
在本文中有2種\。 \ n是一個換行符,另一個用作路徑分隔。 Windows使用\分隔路徑,linux使用/,請注意,在腳本中看到\\的位置,第一個反斜槓將轉義第二個反斜槓。另外,你需要刪除驅動器號('C:')以便在linux中使用。 – Perkins