0

I'm trying to make a python script that converts one file type to another, and I'd like to have the option to specify an output file, but a default to just change the extension on the default file name.

Eg: I want convert('foo.gb') to output foo.faa, but convert('foo.gb', 'bar.faa') to output bar.faa

The way I've implemented this is:

#!/usr/bin/env python

def convert(inputFile, outputFile = None):
    [code that converts data]

    if not outputFile:
        import re
        name = re.match('(.+)\.\w+', inputFile)
        outputFile = './{0}.faa'.format(name.group(1))

    with open(outputFile, 'w+') as outFaa:
        outputFaa.write([stuff to write])

So, it checks to see if an output has been specified, and if it hasn't, uses regular expressions to change the input file name to have the right extension. This code works, but seems somewhat sloppy, or at the very least not very readable. It would also break if the file name has a . anywhere other than before the extension, which it might sometimes have.

Is there a better way to do this?

4

2 回答 2

2

请使用友好人士在您的 Python 安装中提供的电池。看看os.path,尤其是在splitext

然后你可以这样写:

def convert(filename, changeto=None):
    basename, ext = os.path.splitext(in)
    if changeto is None:
        return basename + ".faa"
    return changeto

在您的应用程序中,您需要注意路径结构。我只想对不包含目录分隔符的文件名使用上面的函数。

于 2015-02-12T18:14:26.577 回答
1
import os

if output_fname is None:
    basename, ext = os.path.splitext(input_fname)
    output_fname = basename + ".faa"

请参阅https://docs.python.org/3.4/library/os.path.html上的文档

编辑:还不错;我真正要做的唯一不同的事情是让输出文件名处理函数中的第一件事。这只是让下一个人更容易找出参数的来源以及如何正确使用它们:

import os
def convert(in_fname, out_fname = None):
    # start by sanitizing parameters
    if out_fname is None:
        basename, ext = os.path.splitext(in_fname)
        out_fname = basename + ".faa"

    # read data
    with open(in_fname) as inf:
        data = inf.read()   # or inf.readlines()

    # do conversion
    result = do_conversion(data)

    # write result
    with open(out_fname, "w") as outf:
        outf.write(result)  # or .write("\n".join(result))
于 2015-02-12T18:14:06.290 回答