在 python 中使用变量作为新文件名称的一部分
Using variable as part of name of new file in python
我是 python 的新手,我的 python 脚本 (split_fasta.py) 有问题。这是我的问题的示例:
list = ["1.fasta", "2.fasta", "3.fasta"]
for file in list:
contents = open(file, "r")
for line in contents:
if line[0] == ">":
new_file = open(file + "_chromosome.fasta", "w")
new_file.write(line)
我省略了程序的底部,因为不需要它。我的问题是,当我 运行 这个程序与我的 fasta123 文件在同一个目录时,它工作得很好:
python split_fasta.py *.fasta
但是如果我在不同的目录中并且我希望程序将新文件(例如 1.fasta_chromsome.fasta)输出到我的当前目录...它不会:
python /home/bin/split_fasta.py /home/data/*.fasta
这仍然会在与 fasta 文件相同的目录中创建新文件。我确定这里的问题与此行有关:
new_file = open(file + "_chromosome.fasta", "w")
因为如果我把它改成这样:
new_file = open("seq" + "_chromosome.fasta", "w")
它在我的当前目录中创建了一个输出文件。
我希望这对你们中的一些人有意义,并且我可以得到一些建议。
您提供了旧文件的完整路径以及新名称。所以基本上,如果 file == /home/data/something.fasta
,输出文件将是 file + "_chromosome.fasta"
,即 /home/data/something.fasta_chromosome.fasta
如果您在 file
上使用 os.path.basename
,您将获得文件名(即在我的示例中,something.fasta
)
来自@Adam Smith
You can use os.path.splitext
to get rid of the .fasta
basename, _ = os.path.splitext(os.path.basename(file))
回到代码示例,我看到了很多Python中不推荐的东西。我会详细介绍。
避免隐藏内置名称,例如 list
、str
、int
...它不明确,以后可能会导致潜在问题。
打开文件进行读写时,应使用with
语法。强烈推荐这样做,因为它会小心关闭文件。
with open(filename, "r") as f:
data = f.read()
with open(new_filename, "w") as f:
f.write(data)
如果文件中有空行,line[0] == ...
将导致 IndexError
异常。请改用 line.startswith(...)
。
最终代码:
files = ["1.fasta", "2.fasta", "3.fasta"]
for file in files:
with open(file, "r") as input:
for line in input:
if line.startswith(">"):
new_name = os.path.splitext(os.path.basename(file)) + "_chromosome.fasta"
with open(new_name, "w") as output:
output.write(line)
经常有人来找我说“太可爱了”。并不真地 :)。缩进级别清楚地表明什么是哪个上下文。
我是 python 的新手,我的 python 脚本 (split_fasta.py) 有问题。这是我的问题的示例:
list = ["1.fasta", "2.fasta", "3.fasta"]
for file in list:
contents = open(file, "r")
for line in contents:
if line[0] == ">":
new_file = open(file + "_chromosome.fasta", "w")
new_file.write(line)
我省略了程序的底部,因为不需要它。我的问题是,当我 运行 这个程序与我的 fasta123 文件在同一个目录时,它工作得很好:
python split_fasta.py *.fasta
但是如果我在不同的目录中并且我希望程序将新文件(例如 1.fasta_chromsome.fasta)输出到我的当前目录...它不会:
python /home/bin/split_fasta.py /home/data/*.fasta
这仍然会在与 fasta 文件相同的目录中创建新文件。我确定这里的问题与此行有关:
new_file = open(file + "_chromosome.fasta", "w")
因为如果我把它改成这样:
new_file = open("seq" + "_chromosome.fasta", "w")
它在我的当前目录中创建了一个输出文件。
我希望这对你们中的一些人有意义,并且我可以得到一些建议。
您提供了旧文件的完整路径以及新名称。所以基本上,如果 file == /home/data/something.fasta
,输出文件将是 file + "_chromosome.fasta"
,即 /home/data/something.fasta_chromosome.fasta
如果您在 file
上使用 os.path.basename
,您将获得文件名(即在我的示例中,something.fasta
)
来自@Adam Smith
You can use
os.path.splitext
to get rid of the.fasta
basename, _ = os.path.splitext(os.path.basename(file))
回到代码示例,我看到了很多Python中不推荐的东西。我会详细介绍。
避免隐藏内置名称,例如 list
、str
、int
...它不明确,以后可能会导致潜在问题。
打开文件进行读写时,应使用with
语法。强烈推荐这样做,因为它会小心关闭文件。
with open(filename, "r") as f:
data = f.read()
with open(new_filename, "w") as f:
f.write(data)
如果文件中有空行,line[0] == ...
将导致 IndexError
异常。请改用 line.startswith(...)
。
最终代码:
files = ["1.fasta", "2.fasta", "3.fasta"]
for file in files:
with open(file, "r") as input:
for line in input:
if line.startswith(">"):
new_name = os.path.splitext(os.path.basename(file)) + "_chromosome.fasta"
with open(new_name, "w") as output:
output.write(line)
经常有人来找我说“太可爱了”。并不真地 :)。缩进级别清楚地表明什么是哪个上下文。