我想寫一個小插件來刪除當前文件並關閉活動視圖。 由於某些原因,self.view.file_name()總是返回None。崇高的文字:如何獲取當前視圖的文件名
我是Python的新手,我不知道爲什麼它不能像這樣工作。根據API Reference file_name()返回當前視圖的文件名。
import sublime, sublime_plugin, send2trash
class DeleteCurrentFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
f = self.view.file_name()
if (f is None):
return
send2trash.send2trash(f)
self.view.window().run_command('close')
Dir輸出的(self.view):
[ '類', 'delattr', '字典', 'DOC','格式','getattribute','hash','init',' len個 ' '模塊', '新', '減少', 'reduce_ex', '再版', 'SETATTR', '的sizeof',' STR ,'add_regions','begin_edit','buffer_id','classify','command_history','em_width','encoding','end_edit','erase','','subclasshook','weakref' ,'erase_regions','erase_status','extract_completions','extract_scope','file_name','find' ,find_all,find_all_results,find_by_selector,fold,fold_regions,full_line, inlined_region','insert','is_dirty','is_folded','is_loading','is_read_only','is_scratch','layout_extent','layout_to_text','line','line_endings','line_height','lines' ,'match_selector','meta_info','name','replace','retarget','rowcol','run_command','scope_name','score_selector','sel','set_encoding','set_line_endings' set_name','set_read_only','set_scratch','set_status','set_syntax_file','set_viewport_position','settings','show','show_at_center','size','split_by_newlines','substr','syntax_name' ,'text_point','text_to_layout','unfold','viewport_extent','viewport_position','visible_region','window','word']
控制檯中的任何錯誤消息? –
否。對'(f爲None)'的檢查總是爲真,因此它在到達'send2trash()'之前返回。不檢查'send2trash()'調用失敗。 當我在控制檯執行'view.file_name()'時,它返回正確的路徑。 – Felix
什麼是打印目錄(self.view)的輸出? –