跳过正文
  1. Posts/

python只获取当前目录下的文件夹及文件名

·1 分钟

list = os.listdir(rootdir)#列出目录下的所有文件和目录

1for line in list:
2    filepath = os.path.join(rootdir,line)
3    if os.path.isdir(filepath):#如果filepath是目录
4        print "dir:" + filepath
5    else:
6        print "file:" + filepath

如果需要遍历文件夹下的所以文件,可以使用os.walk()方法。

1os.walk()返回一个三元素的tuple当前路径子文件夹名称文件列表
2import os
3for root, dirs, files in os.walk(path):
4    for filename in files:
5        print filename
6    for dirname in dirs:
7        print dirname

举个列处当前目录所有文件夹的例子:

1from os import listdir
2from os.path import isfile, join
3import os
4
5dir =os.listdir()
6for line in dir:
7  if os.path.isdir(line):
8    print (line)

参考资料

相关文章

conda升级anaconda ValueError的解决办法

·1 分钟
conda update anaconda 后提示 1ValueError: unsupported format character ')' (0x29) at index 49 查到了这个:anaconda update issue I have narrowed this down to the following packages: package build psutil-1.2.1 py27_0 hard-link pycparser-2.10 py27_0 hard-link pykit-0.1.0 np18py27_2 hard-link pyparsing-2.0.1 py27_0 hard-link by calling "conda install anaconda" and then successfully installing everything else one at a time. These four packages consistently exhibit the described behaviour. (note: pykit depends on pycparser so may itself be ok - can't tell) 我先把psutil卸载掉,重新update了一下,成功。

vim下python 的配置

·2 分钟
由于最近要做数字图像处理的大作业,以及之后一段时间,估计写python多一些,所以打算花些时间配置下vim. # 1. 一键执行 # 其实之前一直有的。。不过没有效果,就没有管。发现问题是,python对应的filetype为"python",而不是"py" # 1func! CompileRunGcc() 2 exec "w" 3 if &filetype == 'c' 4 exec "!g++ % -o %<" 5 exec "! ./%<" 6 elseif &filetype == 'cpp' 7 exec "!g++ % -std=gnu++11 -Wall -o %<" 8 exec "! ./%<" 9 elseif &filetype == 'java' 10 exec "!javac %" 11 exec "!java %<" 12 elseif &filetype == 'sh' 13 :!./% 14 elseif &filetype == 'python' 15 " exec "!python %" 16 " exec "!python %<" 17 exec "!python2.7 %" 18 endif 19endfunc 2.代码补全 # 不想折腾了。。既然ycm也支持python,就先用用看好了。。不行再换别的。 # 放一段ycm for python的配置文件 # 1"默认配置文件路径" 2let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' 3"打开vim时不再询问是否加载ycm_extra_conf.py配置" 4let g:ycm_confirm_extra_conf=0 5set completeopt=longest,menu 6"python解释器路径" 7let g:ycm_path_to_python_interpreter='/usr/bin/python' 8"是否开启语义补全" 9let g:ycm_seed_identifiers_with_syntax=1 10"是否在注释中也开启补全" 11let g:ycm_complete_in_comments=1 12let g:ycm_collect_identifiers_from_comments_and_strings = 0 13"开始补全的字符数" 14let g:ycm_min_num_of_chars_for_completion=2 15"补全后自动关机预览窗口" 16let g:ycm_autoclose_preview_window_after_completion=1 17" 禁止缓存匹配项,每次都重新生成匹配项" 18let g:ycm_cache_omnifunc=0 19"字符串中也开启补全" 20let g:ycm_complete_in_strings = 1 3. 语法检查 # Syntastic大家都知道了。。。。看到了异步检测插件ALE,打算试一下。 # ale_github # 需要注意的是,这个插件需要vim 8.0+的特性。。。 # 放一波配置文件 # 1""""""""""""""""""for ale begin """""""""""""""" 2let g:ale_sign_column_always = 1 "保持侧边栏可见: 3 4let g:ale_sign_error = '>>' "改变错误和警告标识符 5let g:ale_sign_warning = '--' 6let g:ale_statusline_format = ['⨉ %d', '⚠ %d', '⬥ ok'] "改变状态栏信息格式 7 8"自定义跳转错误行快捷键: 9nmap <silent> <C-k> <Plug>(ale_previous_wrap) 10nmap <silent> <C-j> <Plug>(ale_next_wrap) 11"消除某excption not caught的警告 12let g:ale_emit_conflict_warnings = 0 4. 编程提示(jedi-vim) # 据说是vim写python的神器。。。装来看看。。。 # 据说默认配置就够了,先不折腾了 # # #