简体   繁体   English

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

[英]How to open every file in a folder

I have a python script parse.py, which in the script open a file, say file1, and then do something maybe print out the total number of characters.我有一个 python 脚本 parse.py,它在脚本中打开一个文件,比如 file1,然后做一些事情可能会打印出字符总数。

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

Right now, I am using stdout to direct the result to my output file - output现在,我正在使用标准输出将结果定向到我的 output 文件 - output

python parse.py >> output

However, I don't want to do this file by file manually, is there a way to take care of every single file automatically?但是,我不想手动逐个文件地执行此文件,有没有办法自动处理每个文件? Like喜欢

ls | awk '{print}' | python parse.py >> output 

Then the problem is how could I read the file name from standardin?那么问题是我怎么能从标准中读取文件名呢? or there are already some built-in functions to do the ls and those kind of work easily?或者已经有一些内置函数可以轻松完成 ls 和那些工作?

Thanks!谢谢!

Os操作系统

You can list all files in the current directory using os.listdir :您可以使用os.listdir列出当前目录中的所有文件:

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全球

Or you can list only some files, depending on the file pattern using the glob module:或者您可以仅列出一些文件,具体取决于使用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

It doesn't have to be the current directory you can list them in any path you want:它不必是当前目录,您可以在任何您想要的路径中列出它们:

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

Pipe管道

Or you can even use the pipe as you specified using fileinput或者您甚至可以使用文件输入指定的fileinput

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

And you can then use it with piping:然后您可以将它与管道一起使用:

ls -1 | python parse.py

You should try using os.walk .您应该尝试使用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

I was looking for this answer:我一直在寻找这个答案:

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))

you can choose as well '*.txt' or other ends of your filename您也可以选择 '*.txt' 或文件名的其他结尾

You can actually just use os module to do both:您实际上可以只使用os 模块来做这两个:

  1. list all files in a folder列出文件夹中的所有文件
  2. sort files by file type, file name etc.按文件类型、文件名等对文件进行排序。

Here's a simple example:这是一个简单的例子:

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

Now you have not only listed all the files in a folder but also have them (optionally) sorted by starting name, file type and others.现在,您不仅列出了文件夹中的所有文件,而且还(可选)按起始名称、文件类型等对它们进行了排序。 Just now iterate over each list and do your stuff.现在遍历每个列表并做你的事情。

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('')

The code below reads for any text files available in the directory which contains the script we are running.下面的代码读取包含我们正在运行的脚本的目录中可用的任何文本文件。 Then it opens every text file and stores the words of the text line into a list.然后它打开每个文本文件并将文本行的单词存储到一个列表中。 After store the words we print each word line by line存储单词后,我们逐行打印每个单词

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)

If you would like to open files in a directory and append them into a list, do this:如果您想打开目录中的文件并将 append 放入列表中,请执行以下操作:

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

you may try another approach of using os.walk and os.path.join which is a little different from the above options:您可以尝试使用 os.walk 和 os.path.join 的另一种方法,这与上述选项略有不同:

for root, dirs, files in os.walk(EnterYourPath):
    for name in files:
        with open(os.path.join(root,name))as f:
            text = f.read()

text variable includes all the files in the folder in the directory. text 变量包括目录中文件夹中的所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM