task3-字典、元组、布尔类型、读写文件.md

This commit is contained in:
Datairon 2023-06-20 00:18:02 +08:00 committed by GitHub
parent 9d485c0426
commit d93eecd08a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 38 deletions

View File

@ -220,39 +220,34 @@ True
  让我们用这些命令做一个小小的编辑器:   让我们用这些命令做一个小小的编辑器:
```python ```python
1 from sys import argv #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 script, filename = argv file = open('d:/b.txt',mode = 'w') # open('路径和文件名,模式= 'XX')这个格式是打开一个文件,如果没有会自动创建;路径可以修改为自己需要的路径,线上的编辑器需要改为线上的路径名
4 file.write('你好,\n 世界。')
5 print(f"We're going to erase {filename}.") file.close()
6 print("If you don't want that, hit CTRL-C (^C).")
7 print("If you do want that, hit RETURN.") target = open('d:/b.txt',mode = 'w')
8
9 input("?") print("Truncating the file. Goodbye!")
10 target.truncate() # 这行命令会把之前的输入都清空
11 print("Opening the file...")
12 target = open(filename, 'w') print("Now I'm going to ask you for three lines.")
13
14 print("Truncating the file. Goodbye!") line1 = input("line 1: ") # 运行后输入 Mary had a little lamb
15 target.truncate() line2 = input("line 2: ") # 运行后输入 Its fleece was white as snow
16 line3 = input("line 3: ") # 运行后输入 It was also tasty
17 print("Now I'm going to ask you for three lines.")
18 print("I'm going to write these to the file.")
19 line1 = input("line 1: ")
20 line2 = input("line 2: ") target.write(line1)
21 line3 = input("line 3: ") target.write("\n")
22 target.write(line2)
23 print("I'm going to write these to the file.") target.write("\n")
24 target.write(line3)
25 target.write(line1) target.write("\n")
26 target.write("\n")
27 target.write(line2) print("And finally, we close it.")
28 target.write("\n") target.close()
29 target.write(line3)
30 target.write("\n")
31
32 print("And finally, we close it.")
33 target.close()
``` ```
  这真是一个很大的文件,可能是你输入过的最大的文件了。所以慢一点,写完检查一下,然后再运行。你也可以写一点运行一点,比如先运行 1-8 行,然后再多运行 5 行,然后再多几行,直到所有的都完成和运行了。   这真是一个很大的文件,可能是你输入过的最大的文件了。所以慢一点,写完检查一下,然后再运行。你也可以写一点运行一点,比如先运行 1-8 行,然后再多运行 5 行,然后再多几行,直到所有的都完成和运行了。
@ -260,11 +255,7 @@ True
  事实上你应该看到两样东西,首先是你新脚本的输出结果:   事实上你应该看到两样东西,首先是你新脚本的输出结果:
```text ```text
$ python3.6 ex16.py test.txt We're going to erase test.txt. Truncating the file. Goodbye!
If you don't want that, hit CTRL-C (^C). If you do want that, hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines. Now I'm going to ask you for three lines.
line 1: Mary had a little lamb line 1: Mary had a little lamb
line 2: Its fleece was white as snow line 2: Its fleece was white as snow