如何打开文件夹中的每个文件

我有一个 python 脚本 parse.py,它在脚本中打开一个文件,比如 file1,然后执行一些操作,可能会打印出字符总数。

filename = 'file1'
f = open(filename, 'r')
content = f.read()
print filename, len(content)

现在,我使用 stdout 将结果定向到输出文件-输出

python parse.py >> output

但是,我不想手动处理这个文件,是否有一种方法可以自动处理每个文件?喜欢

ls | awk '{print}' | python parse.py >> output
那么问题是我如何从标准中读取文件名呢? 还是已经有一些内置函数可以轻松地完成 ls 和那些工作

谢谢!

552162 次浏览

O

你可使用以下工作目录列出所有档案:

import os
for filename in os.listdir(os.getcwd()):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff

地球仪

或者,根据使用 glob模块的文件模式,您只能列出一些文件:

import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff

它不一定是你可以在任意路径中列出的工作目录:

import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff

烟斗

或者甚至可以按照使用 fileinput指定的方式使用管道

import fileinput
for line in fileinput.input():
# do your stuff

然后你可以把它和管道一起使用:

ls -1 | python parse.py

您应该尝试使用 os.walk

import os


yourpath = 'path'


for root, dirs, files in os.walk(yourpath, topdown=False):
for name in files:
print(os.path.join(root, name))
stuff
for name in dirs:
print(os.path.join(root, name))
stuff

你实际上可以使用 < a href = “ https://docs.python.org/2/library/os.html”rel = “ noReferrer”> os 模块 来同时做这两件事:

  1. 列出文件夹中的所有文件
  2. 按文件类型、文件名等对文件进行排序。

这里有一个简单的例子:

import os #os module imported here
location = os.getcwd() # get present working directory location here
counter = 0 #keep a count of all files found
csvfiles = [] #list to store all csv files found at location
filebeginwithhello = [] # list to keep all files that begin with 'hello'
otherfiles = [] #list to keep any other file that do not match the criteria


for file in os.listdir(location):
try:
if file.endswith(".csv"):
print "csv file found:\t", file
csvfiles.append(str(file))
counter = counter+1


elif file.startswith("hello") and file.endswith(".csv"): #because some files may start with hello and also be a csv file
print "csv file found:\t", file
csvfiles.append(str(file))
counter = counter+1


elif file.startswith("hello"):
print "hello files found: \t", file
filebeginwithhello.append(file)
counter = counter+1


else:
otherfiles.append(file)
counter = counter+1
except Exception as e:
raise e
print "No files found here!"


print "Total files found:\t", counter

现在您不仅列出了一个文件夹中的所有文件,而且还可以(可选地)根据起始名称、文件类型和其他进行排序。现在迭代每个列表,做你的事情。

我一直在寻找这个答案:

import os,glob
folder_path = '/some/path/to/file'
for filename in glob.glob(os.path.join(folder_path, '*.htm')):
with open(filename, 'r') as f:
text = f.read()
print (filename)
print (len(text))

您也可以选择’* . txt’或文件名的其他末尾

import pyautogui
import keyboard
import time
import os
import pyperclip


os.chdir("target directory")


# get the current directory
cwd=os.getcwd()


files=[]


for i in os.walk(cwd):
for j in i[2]:
files.append(os.path.abspath(j))


os.startfile("C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe")
time.sleep(1)




for i in files:
print(i)
pyperclip.copy(i)
keyboard.press('ctrl')
keyboard.press_and_release('o')
keyboard.release('ctrl')
time.sleep(1)


keyboard.press('ctrl')
keyboard.press_and_release('v')
keyboard.release('ctrl')
time.sleep(1)
keyboard.press_and_release('enter')
keyboard.press('ctrl')
keyboard.press_and_release('p')
keyboard.release('ctrl')
keyboard.press_and_release('enter')
time.sleep(3)
keyboard.press('ctrl')
keyboard.press_and_release('w')
keyboard.release('ctrl')
pyperclip.copy('')

下面的代码读取包含我们正在运行的脚本的目录中可用的任何文本文件。然后它打开每个文本文件并将文本行中的单词存储到一个列表中。存储单词后,我们逐行打印每个单词

import os, fnmatch


listOfFiles = os.listdir('.')
pattern = "*.txt"
store = []
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
_fileName = open(entry,"r")
if _fileName.mode == "r":
content = _fileName.read()
contentList = content.split(" ")
for i in contentList:
if i != '\n' and i != "\r\n":
store.append(i)


for i in store:
print(i)

如果您希望打开目录中的文件并将它们附加到列表中,请执行以下操作:

mylist=[]
for filename in os.listdir('path/here/'):
with open(os.path.join('path/here/', filename), 'r') as f:
mylist.append(f.read())