Új hozzászólás Aktív témák

  • sztikac

    őstag

    Sziasztok, a Learn Python 3 The Hard Way könyvből tanulom a Python-t és van ez a feladat:

    from sys import argv
    from os.path import exists

    script, from_file, to_file = argv

    print(f"Copying from {from_file} to {to_file}")

    # we could do these two on one line, how?
    in_file = open(from_file)
    indata = in_file.read()

    print(f"The input file is {len(indata)} bytes long")

    print(f"Does the output file exist? {exists(to_file)}")
    print("Ready, hit RETURN to continue, CTRL-C to abort.")
    input()

    out_file = open(to_file, 'w')
    out_file.write(indata)
    print("Alright, all done.")

    out_file.close()
    in_file.close()

    Ezt teljesen világos is, hogy mit-miért csinál.
    Aztán van ugye a feladatok között egy ilyen:
    See how short you can make the script. I could make this one line long.

    Ezzel eddig jutottam (még elég messze vagyok az 1 sortól :U):

    from sys import argv
    script, from_file, to_file = argv
    in_data = open(from_file).read()
    open(to_file, 'w').write(in_data)

    Ez így lefut, rendben le is másolja a megadott fájlt, csak ugye ebben az esetben nincs file descriptorom így nem tudom min meghívni a close()-t. Úgy tudom a program lefutása után az OS úgyis bezárja a nyitott fájlokat, de szabályos ez így?

Új hozzászólás Aktív témák