小编Re-*_*e-l的帖子

Python - 'str'对象没有属性'close'

嗨,大家好.我正在努力弄清楚为什么不需要成为我编写的这几行代码的结束属性:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

file_content = open(from_file).read()
new_file = open(to_file, 'w').write(file_content)

new_file.close()

file_content.close()
Run Code Online (Sandbox Code Playgroud)

我读了一些关于这个的事情和其他人的帖子,但他们的脚本比我目前学习的要复杂得多,所以我无法弄清楚为什么.

我正在努力学习Python,并感谢任何帮助.

先感谢您.

python file-io

13
推荐指数
1
解决办法
1万
查看次数

使用函数进行字符串操作(替换和反向)

所有!

新手在这里!我试图创建一个允许我替换四个字符(ACTG)的函数.为了更简洁,我想用'T'代替'A',用'G'代替'C',反之亦然.

我到目前为止的代码是这样的,但我得到一个错误(翻译预期至少有1个参数,得到0).

#!/usr/bin/python
from sys import argv
from os.path import exists

def translate():
    str.replace("A", "T");
    str.replace("C", "G");
    str.replace("G", "C");
    str.replace("T", "A");

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

in_file = open(from_file)
indata = in_file.read()

newdata = indata.translate()

out_file = open(to_file, 'w')
out_file.write(newdata[::-1])

out_file.close()
in_file.close()
Run Code Online (Sandbox Code Playgroud)

我尝试给translate函数一个argument(def translate(str))并用str(newdata = indata.translate(str))调用它,但那些也没用.

将不胜感激任何帮助和指导.

python function str-replace

0
推荐指数
1
解决办法
182
查看次数

标签 统计

python ×2

file-io ×1

function ×1

str-replace ×1