您将如何遍历目录并使用 For 循环从中获取文件?
How Would You Go Through a Directory and Get the Files From Them With a For Loop?
我想从一个目录中获取所有文件并逐个编辑它们,但我不知道该怎么做。我尝试查看问题 here,但答案并不是我所需要的。这是我的代码:
import os
playeritems = 'PlayerFiles/PlayerItems'
for files in os.walk(playeritems):
for file in files:
with open(os.path.join(file), 'r') as t:
t_reading = t.readlines()
for i in t_reading:
#Do what I need to do to the line of code.
我试图查看每个文件并更改其中的一些行,但它一直给我错误:
IsADirectory: Is a directory on with open(os.path.join(file), 'r') as t:
我当前的代码是从上面的问题中得到的,所以如果其中还有其他错误,我会在找到它们后尽快修复它们。
我不包括在 playeritems
中查找的子目录可能性或目录可能性。我的代码应该如下所示:
for _, __, files in os.walk(playeritems):
for file in files:
with open(os.path.join(playeritems, file), 'r') as t:
#Do stuff to the file
我想从一个目录中获取所有文件并逐个编辑它们,但我不知道该怎么做。我尝试查看问题 here,但答案并不是我所需要的。这是我的代码:
import os
playeritems = 'PlayerFiles/PlayerItems'
for files in os.walk(playeritems):
for file in files:
with open(os.path.join(file), 'r') as t:
t_reading = t.readlines()
for i in t_reading:
#Do what I need to do to the line of code.
我试图查看每个文件并更改其中的一些行,但它一直给我错误:
IsADirectory: Is a directory on
with open(os.path.join(file), 'r') as t:
我当前的代码是从上面的问题中得到的,所以如果其中还有其他错误,我会在找到它们后尽快修复它们。
我不包括在 playeritems
中查找的子目录可能性或目录可能性。我的代码应该如下所示:
for _, __, files in os.walk(playeritems):
for file in files:
with open(os.path.join(playeritems, file), 'r') as t:
#Do stuff to the file