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

时间:2021-01-05 00:30:23

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

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

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?

那么问题是如何从standardin读取文件名?或者已经有一些内置函数可以轻松完成ls和那些工作?

Thanks!

谢谢!

6 个解决方案

#1


212  

You can list all files in the current directory using:

您可以使用以下命令列出当前目录中的所有文件:

import os
for filename in os.listdir(os.getcwd()):
   # do your stuff

Or you can list only some files, depending on the file pattern using the glob module:

或者您可以只列出一些文件,具体取决于使用glob模块的文件模式:

import glob
for filename in glob.glob('*.txt'):
   # do your stuff

It doesn't have to be the current directory you can list them in any path you want:

它不必是您可以在任何所需路径中列出它们的当前目录:

path = '/some/path/to/file'

for filename in os.listdir(path):
    # do your stuff

for filename in glob.glob(os.path.join(path, '*.txt')):
    # do your stuff

Or you can even use the pipe as you specified using fileinput

或者您甚至可以使用fileinput指定的管道

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

And then use it with piping:

然后用它来管道:

ls -1 | python parse.py

#2


22  

you should try using os.walk

你应该尝试使用os.walk

yourpath = 'path'

import os
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

#3


4  

You can actually just use os module to do both:

实际上你可以使用os模块来做到这两点:

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

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.

现在,您不仅列出了文件夹中的所有文件,还将它们(可选)按起始名称,文件类型等进行排序。刚才迭代每个列表并做你的东西。

#4


1  

Easy Solution

If you want to just open all the files in the root of a directory. I've run across this problem a lot of times so I created an easy to use module for both Python 3.5 and Python 2.7. If your Python version is not supported just ask me on GreyCadet IRC and I will add the support.

如果您只想打开目录根目录中的所有文件。我经常遇到这个问题所以我为Python 3.5和Python 2.7创建了一个易于使用的模块。如果您的Python版本不受支持,请在GreyCadet IRC上询问我,我将添加支持。

Installing the module

pip install filemapper

Usage

Consider a directory structure like this and that main.py is your code.

考虑这样的目录结构,main.py就是你的代码。

-Program
    -resources
        nouns.txt
        config.dat
        help.txt
     main.py

Contents of main.py

main.py的内容

import filemapper as fm
all_files = fm.load('resources') # fm.load('resources','w') will open in write mode
for f in all_files:
    for i in fm.read(f):print i

This will print out the lines of each file in the resources folder. You can also pass any mode.

这将打印出resources文件夹中每个文件的行。您也可以通过任何模式。

Doing more

If you want to do more than just open files using this module then visit the filemapper GitHub Page for more details.

如果您想做的不仅仅是使用此模块打开文件,请访问filemapper GitHub页面以获取更多详细信息。

#5


1  

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'或文件名的其他两端

#6


0  

import pyautogui
import keyboard
import time
import os
import pyperclip

os.chdir("target 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('')

#1


212  

You can list all files in the current directory using:

您可以使用以下命令列出当前目录中的所有文件:

import os
for filename in os.listdir(os.getcwd()):
   # do your stuff

Or you can list only some files, depending on the file pattern using the glob module:

或者您可以只列出一些文件,具体取决于使用glob模块的文件模式:

import glob
for filename in glob.glob('*.txt'):
   # do your stuff

It doesn't have to be the current directory you can list them in any path you want:

它不必是您可以在任何所需路径中列出它们的当前目录:

path = '/some/path/to/file'

for filename in os.listdir(path):
    # do your stuff

for filename in glob.glob(os.path.join(path, '*.txt')):
    # do your stuff

Or you can even use the pipe as you specified using fileinput

或者您甚至可以使用fileinput指定的管道

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

And then use it with piping:

然后用它来管道:

ls -1 | python parse.py

#2


22  

you should try using os.walk

你应该尝试使用os.walk

yourpath = 'path'

import os
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

#3


4  

You can actually just use os module to do both:

实际上你可以使用os模块来做到这两点:

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

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.

现在,您不仅列出了文件夹中的所有文件,还将它们(可选)按起始名称,文件类型等进行排序。刚才迭代每个列表并做你的东西。

#4


1  

Easy Solution

If you want to just open all the files in the root of a directory. I've run across this problem a lot of times so I created an easy to use module for both Python 3.5 and Python 2.7. If your Python version is not supported just ask me on GreyCadet IRC and I will add the support.

如果您只想打开目录根目录中的所有文件。我经常遇到这个问题所以我为Python 3.5和Python 2.7创建了一个易于使用的模块。如果您的Python版本不受支持,请在GreyCadet IRC上询问我,我将添加支持。

Installing the module

pip install filemapper

Usage

Consider a directory structure like this and that main.py is your code.

考虑这样的目录结构,main.py就是你的代码。

-Program
    -resources
        nouns.txt
        config.dat
        help.txt
     main.py

Contents of main.py

main.py的内容

import filemapper as fm
all_files = fm.load('resources') # fm.load('resources','w') will open in write mode
for f in all_files:
    for i in fm.read(f):print i

This will print out the lines of each file in the resources folder. You can also pass any mode.

这将打印出resources文件夹中每个文件的行。您也可以通过任何模式。

Doing more

If you want to do more than just open files using this module then visit the filemapper GitHub Page for more details.

如果您想做的不仅仅是使用此模块打开文件,请访问filemapper GitHub页面以获取更多详细信息。

#5


1  

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'或文件名的其他两端

#6


0  

import pyautogui
import keyboard
import time
import os
import pyperclip

os.chdir("target 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('')