我是passtime程序員,所以請溫和。現在對於實際的問題,我的一個用戶遇到了這種奇怪的行爲,其中os.path.join(p1,p2)
返回一個相對路徑,並且從p1
中省略了所有斜線。像這樣(假裝這是Python CMD行解釋器中完成):os.path.join(p1,p2)省略了p1的斜槓
>>import os
>>p1 = "/Some/Path/Tosmth"
>>p2 = "file.ext"
>>print os.path.join(p1,p2)`
然後輸出爲:
>>"SomePathTosmth/file.ext"
就在連接操作我檢查P1和P2的內容,它是正是我所期望的。這是有問題的實際執行有一些額外的調試代碼:
def __moveMovie(self, src, dst, folder, file_name):
try:
self.logDebug('__moveMovie(): src=%s | dst=%s | folder=%s | file_name=%s' % (src, dst, folder, file_name))
dest = save_path(dst)
file_name = save_path(file_name)
if self.getConfig("subfolder") is True:
dest = os.path.join(dst,folder)
os.mkdir(Utils().encode(dest))
except OSError, e:
if e.args[0] == 17:
self.logDebug(u'Cannot create folder "%s". It already exists.' % os.path.join(dest))
try:
full_dst = Utils().encode(os.path.join(dest,file_name))
self.logDebug('var "full_dst" w/o encode: %s' % os.path.join(dest, file_name))
self.logDebug('var "full_dst" w/ encode: %s' % Utils().encode(os.path.join(dest,file_name)))
if os.path.exists(full_dst):
pass
shutil.move(src, full_dst)
self.logInfo(u'Movie "%s" moved to "%s"' % (os.path.split(src)[1], os.path.join(dest,file_name)))
self.__movie_queue.task_done()
except OSError, e:
if e.args[0] == 21:
self.logDebug(u'Cannot move "%s" to "%s". "%s" is a directory.' % (os.path.split(src)[1],
os.path.join(dest, file_name),
os.path.join(dest, file_name)))
self.__movie_queue.task_done()
這是日誌代碼:
05.06.2013 17:29:12 DEBUG MovieMover: __moveMovie(): src=/var/raid/Daten/Neu/abgezockt.tc.72-ps/Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft.mkv | dst=/var/raid/Daten/Filme | folder=Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft | file_name=Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/o encode: varraidDatenFilme/Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/ encode: varraidDatenFilme/Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: __moveMovie(): src=/var/raid/Daten/Neu/abgezockt.tc.72-ps/.AppleDouble/Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft.mkv | dst=/var/raid/Daten/Filme | folder=Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft | file_name=Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/o encode: varraidDatenFilme/Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/ encode: varraidDatenFilme/Voll Abgezockt.mkv
Exception in thread Thread-1072:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 360, in __getMvQueue
self.__moveMovie(src, dst, movie.folder_name, movie.file_name)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 382, in __moveMovie
shutil.move(src, full_dst)
File "/usr/lib/python2.7/shutil.py", line 301, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: u'varraidDatenFilme/Voll Abgezockt.mkv'
Exception in thread Thread-1074:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 360, in __getMvQueue
self.__moveMovie(src, dst, movie.folder_name, movie.file_name)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 382, in __moveMovie
shutil.move(src, full_dst)
File "/usr/lib/python2.7/shutil.py", line 301, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: u'varraidDatenFilme/Voll Abgezockt.mkv'
我是因爲編碼功能完全喪失封閉os.path中.join()函數不是原因,當他用他的python cmd行解釋器執行os.path.join()時,結果是正確的。如果有任何興趣的話,我在2.7上執行代碼時寫入了2.5。我希望有人能夠闡明這一點。謝謝!
因爲它似乎我不能使用代碼中的註釋格式在這裏我要回答這些:
這是的save_path()函數:
def save_path(name):
#remove some chars
if os.name == 'nt':
return remove_chars(name, '/\\?%*:|"<>')
else:
return remove_chars(name, '/\\"')
這就是現在我必須補充不我的代碼。我正在寫一個名爲pyLoad的開源項目。只是想確保我沒有聲稱任何東西是我的代碼,但事實並非如此。
編輯:布倫丹朗的答案似乎是準確的。在我的開發環境中,self.getConfig("subfolder")
處於開啓狀態,同時顯示它不是我的用戶。有了這個,我可以成功地重現錯誤。我會釋放該修復程序,並讓有問題的用戶確認它的工作原理,但到目前爲止,它都指向save_path
是罪魁禍首。也覺得有點像一個白癡俯視明顯,真的。無論如何,謝謝,我會回來給你結果。
什麼是'save_path'? –
我將它添加到實際問題中。 –
第一部分在2.7工作正常 – Serial