在做了一些研究之后,我对错误是什么有了更好的认识。请原谅我,如果我错了,但它与你所连接的管道/插座有关,然后才能传输所有数据?我不清楚解决这个问题的最佳方法。你如何防止它过早关闭?我的程序连接到网络附加存储设备,检查是否存在某些文件夹(如果没有创建它们),然后将图像和mp3移动到这些文件夹。传输大量文件时,错误似乎最常发生。我试过的最后一个包括64个轨道和15个各种尺寸的图像。最大的是180kb。而每一步我都得到了破裂的管道错误。我在应用程序启动时连接到服务器一次。我正在使用PySMB来做到这一点。我知道可能有一些方法可以优化这段代码,这也可能是问题的一部分。
以下是我的一些代码。用于移动图像并检查存储图像的文件夹是否存在于NAS上。
def split_into_folders(art, first_check):
"""Alphabetically organizes a string into folders based on the character
it starts with and creates those folders if they don't already exist.
:param art: the jpg file containing the album art itself as well as
metadata about it
"""
service_name = "websites"
covers_folder = "Covers"
original = "original"
small = 75
medium = 155
large = 256
default = 300
if _platform == "darwin" or _platform == "linux" or _platform == "linux2":
a_f = "/a-f/"
g_m = "/g-m/"
n_s = "/n-s/"
t_z = "/t-z/"
slash = "/"
else:
a_f = "\\a-f\\"
g_m = "\\g-m\\"
n_s = "\\n-s\\"
t_z = "\\t-z\\"
slash = "\\"
if first_check:
# Checks if a particular folder on the service_name exists and if it
# doesn't creates it.
print("Covers folder exists" + " | " + str(does_directory_exist(
service_name, slash, covers_folder)))
# Checks to see if 75 (small), 155 (medium), 255 (large), 300 (default)
# and original folders exists and if they don't creates them. Using
# slash very loosely here to accomplish my checks.
covers_folder = covers_folder + slash
print("Small folder exists" + " | " + str(does_directory_exist(
service_name, covers_folder, str(small))))
print("Medium folder exists" + " | " + str(does_directory_exist(
service_name, covers_folder, str(medium))))
print("Large folder exists" + " | " + str(does_directory_exist(
service_name, covers_folder, str(large))))
print("Default folder exists" + " | " + str(does_directory_exist(
service_name, covers_folder, str(default))))
print("Original folder exists" + " | " + str(does_directory_exist(
service_name, covers_folder, original)))
if art is not None:
s = os.path.basename(art)
size = Image.open(art)
covers_folder = covers_folder + slash
if int(size.size[1]) == small:
album_art_size_folder = str(small)
elif int(size.size[1]) == medium:
album_art_size_folder = str(medium)
elif int(size.size[1]) == large:
album_art_size_folder = str(large)
elif int(size.size[1]) == default:
album_art_size_folder = str(default)
else:
album_art_size_folder = original
if s[0].lower() >= "g" and s[0].lower() <= "m":
album_art_size_destination_folder = covers_folder + \
album_art_size_folder + slash
print(
"g-m folder exists" + " | " +
str(does_directory_exist(
service_name, album_art_size_destination_folder,
g_m.replace("/", "").replace("\\", ""))))
album_art_size_destination_folder = covers_folder + \
album_art_size_folder
return album_art_size_destination_folder + g_m, service_name
elif s[0].lower() >= "n" and s[0].lower() <= "s":
album_art_size_destination_folder = \
covers_folder + album_art_size_folder + slash
print(
"n-s folder exists" + " | " +
str(does_directory_exist(
service_name, album_art_size_destination_folder,
n_s.replace("/", "").replace("\\", ""))))
album_art_size_destination_folder = covers_folder + \
album_art_size_folder
return album_art_size_destination_folder + n_s, service_name
elif s[0].lower() >= "t" and s[0].lower() <= "z":
album_art_size_destination_folder = \
covers_folder + album_art_size_folder + slash
print(
"t-z folder exists" + " | " +
str(does_directory_exist(
service_name, album_art_size_destination_folder,
t_z.replace("/", "").replace("\\", ""))))
album_art_size_destination_folder = covers_folder + \
album_art_size_folder
return album_art_size_destination_folder + t_z, service_name
else:
album_art_size_destination_folder = \
covers_folder + album_art_size_folder + slash
print(
"a-album_art_size_destination_folder folder exists" + " | " +
str(does_directory_exist(
service_name, album_art_size_destination_folder,
a_f.replace("/", "").replace("\\", ""))))
album_art_size_destination_folder = covers_folder + \
album_art_size_folder
return album_art_size_destination_folder + a_f, service_name
else:
return covers_folder, service_name
def does_directory_exist(service_name, path, directory_check):
"""
:param service_name: the name of the shared folder for the path,
should be a string or unicode
:param path: path relative to the service_name where we are
interested to learn about its files/sub-folders,
should be a string or unicode
:param directory_check: the directory or file we are checking whether or
not exists, should be a string
:returns: a boolean, either true if the directory or file already exists
or false if a new one needed to be created
"""
global IS_CONNECTED
# Get a list of all of the items (directories and files)
# in the shared folder.
try:
results_list = CONN.listPath(service_name, path, timeout=6000)
for item in results_list:
if directory_check == item.filename and item.isDirectory:
# If the directory or file we're looking for already exists on
# the network storage device than we're golden.
return True
elif item is results_list[-1] and directory_check != item.filename:
# If the last item in the shared folder is still not the file
# we are looking for than that means we need to create it.
try:
CONN.createDirectory(
service_name, "%s" % (
path + directory_check), timeout=6000)
except BaseException as exception:
print("does_directory_exist" + " | createDirectory | "
"" + str(exception))
return False
except BaseException as exception:
print("does_directory_exist" + " | results_list | " + str(exception))