February 2024
Beginner
352 pages
8h 16m
English
Now let’s do a few more things with files. We’ll write a Python script to copy one file to another. It’ll be very short but will give you ideas about other things you can do with files.
Listing 17.1: ex17.py
1 from sys import argv
2 from os.path import exists
3
4 from_file = "test.txt"
5 to_file = "new_test.txt"
6
7 print(f"Copying from {from_file} to {to_file}")
8
9 # we could do these two on one line, how?
10 in_file = open(from_file)
11 indata = in_file.read()
12
13 print(f"The input file is {len(indata)} bytes long")
14
15 print(f"Does the output file exist? {exists(to_file)}")
16 print("Ready, hit RETURN to continue, CTRL-C to abort.")
17 input()
18
19 out_file = open(to_file, 'w'Read now
Unlock full access