这就是我所拥有的:
glob(os.path.join('src','*.c'))
Run Code Online (Sandbox Code Playgroud)
但我想搜索src的子文件夹.像这样的东西会起作用:
glob(os.path.join('src','*.c'))
glob(os.path.join('src','*','*.c'))
glob(os.path.join('src','*','*','*.c'))
glob(os.path.join('src','*','*','*','*.c'))
Run Code Online (Sandbox Code Playgroud)
但这显然是有限和笨重的.
如何获取Python中给定目录中所有文件(和目录)的列表?
我正在尝试编写一个简单的Python脚本,它将index.tpl复制到所有子目录中的index.html(除了少数例外).
我试图获取子目录列表而陷入困境.
如何限制os.walk只返回我提供的目录中的文件?
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
for f in files:
if os.path.splitext(f)[1] in whitelist:
outputList.append(os.path.join(root, f))
else:
self._email_to_("ignore")
return outputList
Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本来递归遍历主文件夹中的子文件夹并构建一个特定文件类型的列表.我遇到了脚本问题.目前设定如下
for root, subFolder, files in os.walk(PATH):
for item in files:
if item.endswith(".txt") :
fileNamePath = str(os.path.join(root,subFolder,item))
Run Code Online (Sandbox Code Playgroud)
问题是subFolder变量正在拉入子文件夹列表而不是ITEM文件所在的文件夹.我想在之前为子文件夹运行for循环并加入路径的第一部分,但我想我会仔细检查以确定是否有人在此之前有任何建议.谢谢你的帮助!
我经常使用python来处理数据目录.最近,我注意到列表的默认顺序已经变为几乎荒谬的东西.例如,如果我在包含以下子目录的当前目录中:run01,run02,... run19,run20,然后我从以下命令生成一个列表:
dir = os.listdir(os.getcwd())
Run Code Online (Sandbox Code Playgroud)
然后我通常按此顺序获得一个列表:
dir = ['run01', 'run18', 'run14', 'run13', 'run12', 'run11', 'run08', ... ]
Run Code Online (Sandbox Code Playgroud)
等等.订单曾经是字母数字.但是这个新订单现在已经和我保持了一段时间.
什么是确定这些列表的(显示)顺序?
我试图获取代码列出文件夹中的所有目录,将目录更改为该文件夹并获取当前文件夹的名称.到目前为止我的代码是在下面,并且不是在工作.我似乎得到父文件夹名称.
import os
for directories in os.listdir(os.getcwd()):
dir = os.path.join('/home/user/workspace', directories)
os.chdir(dir)
current = os.path.dirname(dir)
new = str(current).split("-")[0]
print new
Run Code Online (Sandbox Code Playgroud)
我还在文件夹中有其他文件,但我不想列出它们.我已经尝试了下面的代码,但我还没有工作.
for directories in os.path.isdir(os.listdir(os.getcwd())):
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到我错在哪里?
谢谢
有它的工作,但它似乎有点圆.
import os
os.chdir('/home/user/workspace')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join('/home/user/workspace', dirs)
os.chdir(dir)
current = os.getcwd()
new = str(current).split("/")[4]
print new
Run Code Online (Sandbox Code Playgroud) AttributeError当我使用python文档中的示例代码(这里)时引发了一个问题.示例代码如下:
with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.') and entry.is_file():
print(entry.name)
Run Code Online (Sandbox Code Playgroud)
结果是AttributeError:
D:\Programming>test.py
Traceback (most recent call last):
File "D:\Programming\test.py", line 3, in <module>
with os.scandir() as it:
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)
虽然,分配os.scandir()给变量工作正常.有人能告诉我我错过了什么吗?
有一个目录包含文件夹以及不同格式的文件.
import os
my_list = os.listdir('My_directory')
Run Code Online (Sandbox Code Playgroud)
将返回文件和文件夹名称的完整内容.例如,我可以使用endswith('.txt')方法来选择文本文件名,但是如何获取只是文件夹名称的列表?
我正在尝试自动指定我的脚本之一所需的子目录。这个想法是让脚本在 C: 驱动器中搜索特定名称的文件夹。在我看来,这需要递归搜索功能。计划是检查所有子目录,如果没有一个是所需的目录,则开始搜索当前子目录的子目录
在研究如何做到这一点时,我遇到了这个问题并开始使用os.walk(dir).next()[1]列出目录。这取得了有限的成功。当脚本搜索目录时,它基本上会放弃并中断,从而给出错误StopIteration。下面的示例输出在 中搜索子目录TEST1。
C:\Python27>test.py
curDir: C:\Python27
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'pyinstaller-2.0', 'Scripts', 'tcl', 'TEST1', 'Tools']
curDir: DLLs
[]
curDir: Doc
[]
curDir: include
[]
curDir: Lib
['bsddb', 'compiler', 'ctypes', 'curses', 'distutils', 'email', 'encodings', 'hotshot',
'idlelib', 'importlib', 'json', 'lib-tk', 'lib2to3', 'logging', 'msilib',
'multiprocessing', 'pydoc_data', 'site-packages', 'sqlite3', 'test', 'unittest', 'wsgiref', 'xml']
curDir: bsddb
Traceback (most recent call last):
File "C:\Python27\test.py", line 24, in <module>
if __name__ == "__main__": main()
File "C:\Python27\test.py", …Run Code Online (Sandbox Code Playgroud) python ×11
directory ×3
file ×3
list ×3
os.walk ×2
filesystems ×1
fnmatch ×1
glob ×1
listdir ×1
path ×1
python-3.x ×1
recursion ×1
scandir ×1
subdirectory ×1