Ich lerne Python und ich bin ein super Anfänger! Ich habe gerade eine Übung absolviert und wollte Variationen davon erstellen. Meine Frage ist, wenn die IF/ELSE Anweisung wie kann ich mit vermeiden "out_file = open (to_file, 'w') out_file.write (indata)" zweimal. Vielen Dank im Voraus!Lernen Python: Import existiert
from sys import argv
from os.path import exists
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()
print "The input file is %d bytes long" % len(indata)
if exists(to_file):
print "File already exists, override?"
raw_input()
else:
out_file = open(to_file,'w')
out_file.write(indata)
out_file = open(to_file,'w')
out_file.write(indata)
print"Done."
out_file.close()
in_file.close()
Ich denke, Sie sind auf einen der Hauptgründe gestoßen, warum [Funktionen] (http://anh.cs.luc.edu/python/hands-on/3.1/ handsonHtml/functions.html # function-parameters) existieren :) – jDo
Wie gesagt, ich habe gerade angefangen in die Welt des Codierens einzutauchen. Also bitte, gib mir ein Beispiel, damit ich lernen kann! ;) –