繁体   English   中英

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

[英]How to merge all files of a directory except a specified one?

我想将当前工作目录中所有扩展名为.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)


问题是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'(附加字节模式),这样如果文件存在,它将append信息给它。

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

请让我知道这可不可以帮你!

感谢:D

给出代码的以下部分

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 filename = sys.argv[2]的文件名
    • 如果output.asc是第二个参数,它将根据指定的模式被覆盖( wb )或附加到( ab )。
  2. 打开未使用的文件效率低下
    • 在打开文件之前检查BigTree.asc ,而not in
    • 使用否定条件作为保护子句来扁平化你的代码。
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)

暂无
暂无

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

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