が私のコードではPython 2.xでファイルを正しくできない理由を私は理解していない: オープン()ここで
copy from: asd.txt
File doesn't exist.
File name: test.txt
copy to: dsa.txt
File doesn't exist.
File name: test.py
Traceback (most recent call last):
File "ex17.py", line 17, in <module>
f.write(open(from_file).read())
IOError: [Errno 2] No such file or directory: 'ad.txt'
ターミナル
でfrom os.path import exists
def confirm(file_name):
while not exists(file_name):
print "File doesn't exist."
file_name = raw_input("File name: ")
from_file = raw_input("copy from: ")
confirm(from_file)
to_file = raw_input("copy to: ")
confirm(to_file)
with open(to_file, 'w')as f:
f.write(open(from_file).read())
出力は、なぜそれが間違っを開くんファイル?
修正方法?
そして、私はこれを行うとき:
from_file = raw_input("copy from: ")
while not exists(from_file):
print "File doesn't exist."
from_file = raw_input("File name: ")
は、それはうまく動作します。
コードを少なくするために関数を定義したいのですが、問題が発生します。
私を参照してください。あなたのアプローチのわずかなバリエーションの下で以下の答えをお試しください... –
このコードであなたの目的は何ですか?カントが存在するため、11行(confirm(to_file))を削除することができます。 – Milor123
@ Milor123あなたの助けてくれてありがとう、私はそれを以前に知りませんでした。 –