如何合并除指定目录外的所有目录文件?

2024-09-28 19:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将当前工作目录中扩展名为.asc的所有文件合并到名为outfile.asc的文件中

我的问题是,我不知道如何排除特定的文件("BigTree.asc"),如果目录中有一个"outfile.asc",如何覆盖现有的文件

if len(sys.argv) < 2:
    print("Please supply the directory of the ascii files and an output-file as argument:")
    print("python merge_file.py directory outfile")
    exit()
directory = sys.argv[1]

os.chdir(directory)
currwd = os.getcwd()

filename = sys.argv[2]
fileobj_out = open(filename, "w") 

starttime = time.time()

read_files = glob.glob(currwd+"\*.asc")

with open("output.asc", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            if f == "BigTree.asc":
                continue
            else:
                outfile.write(infile.read())

endtime = time.time()
runtime = int(endtime-starttime)
sys.stdout.write("The script took %i sec." %runtime)



Tags: 文件目录readiftimeassysfiles
2条回答

给出代码的以下部分

with open("output.asc", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            if f == "BigTree.asc":
                continue
            else:
                outfile.write(infile.read())

更新部分

  1. open("output.asc", "wb")filename = sys.argv[2]更改为使用filename
    • 如果output.asc是第二个参数,它将被覆盖(wb)或附加到(ab),具体取决于指定的模式
  2. 打开未使用的文件效率低下
    • 在打开文件之前,使用not in检查BigTree.asc
    • 使用否定条件作为保护子句来扁平化代码
with open(filename, "wb") as outfile:
    for f in read_files:
        if "BigTree.asc" not in f:
            with open(f, "rb") as infile:
                outfile.write(infile.read())

读写模式

| Access Modes | Description                                                   |
|       |                               -|
| r            | Opens a file for reading only.                                |
| rb           | Opens a file for reading only in binary format.               |
| r+           | Opens a file for both reading and writing.                    |
| rb+          | Opens a file for both reading and writing in binary format.   |
| w            | Opens a file for writing only.                                |
| wb           | Opens a file for writing only in binary format.               |
| w+           | Opens a file for both writing and reading.                    |
| wb+          | Opens a file for both writing and reading in binary format.   |
| a            | Opens a file for appending.                                   |
| ab           | Opens a file for appending in binary format.                  |
| a+           | Opens a file for both appending and reading.                  |
| ab+          | Opens a file for both appending and reading in binary format. |

完整程序

  • 这一行fileobj_out = open(filename, "w")应该删除
import glob
import sys
import os
import time


if len(sys.argv) < 2:
    print("Please supply the directory of the ascii files and an output-file as argument:")
    print("python merge_file.py directory outfile")
    exit()
directory = sys.argv[1]

os.chdir(directory)

currwd = os.getcwd()

filename = sys.argv[2]

starttime = time.time()

read_files = glob.glob(currwd+"\*.asc")

with open(filename, "wb") as outfile:  # "wb" or "ab", if you want to append or not
    for f in read_files:
        if "BigTree.asc" not in f:
            with open(f, "rb") as infile:
                outfile.write(infile.read())

endtime = time.time()
runtime = int(endtime-starttime)
sys.stdout.write("The script took %i sec." %runtime)

问题是glob采用的文件名具有完整路径。我做了一些小改动,现在应该可以在你的代码上使用了。例如,不要使用==,而是使用in

if len(sys.argv) < 2:
    print("Please supply the directory of the ascii files and an output-file as argument:")
    print("python merge_file.py directory outfile")
    exit()
directory = sys.argv[1]

os.chdir(directory)

filename = sys.argv[2]
fileobj_out = open(filename, "w") 

starttime = time.time()

read_files = glob.glob(currwd+"\*.asc")

# Change [1]
with open("output.asc", "ab") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            # Change [2] '==' for 'in'
            if "BigTree.asc" in f:
                continue
            else:
                outfile.write(infile.read())

endtime = time.time()
runtime = int(endtime-starttime)
sys.stdout.write("The script took %i sec." %runtime)

解释

[1]将文件模式从“wb”(写入字节模式)更改为“ab”(追加字节模式),这样,如果文件存在,它将向其追加信息

[2]以这种方式更改了“in”的“==”,如果文件名f包含字符串BigTree.asc,它将跳过此文件并继续

请让我知道这是否有帮助

谢谢:D

相关问题 更多 >