Exercise 16: 读写文件

原文链接:http://learnpythonthehardway.org/book/ex16.html

       如果你做了上次练习中的研究训练的话你应该了解了各种文件相关的操作命令(方法 / 函数)了。下面列出的命令我希望你能记住它们:

1> close -- 关闭文件操作。就像在文本编辑器中做File->Save..这个操作一样。

2> read  -- 读取文件中的文本内容。你可以将读到的文本内容赋给一个变量。

3> readline -- 只读取文件中一行文本内容。

4> write(stuff) -- 向文件中写入内容。

       这些都是你目前需要知道重要命令。其中一些命令带有参数设置,但是现在我们还没有必要真正去关心参数的事情。你只要记住 write 带有一个参数,这个参数值就是你想写入文件中字符串。

       让我们就使用其中的一些命令来写一个小型简单的文本编辑器。

from sys import argv

script ,filename = argv

print "We're going to erase %r." % filename
print "If you don't want that ,hit CTRL-C (^C)."
print"If you do want that hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "And finally ,we close it."
target.close()
      这是一个比较长的程序,可能是你输过最长的了。所以你要仔细谨慎的输入,做好检查,然后正确运行。一个检查的技巧就是你可以写完一小段就运行检查一次。你可以输入完1-8行后就运行一次,然后再添加五行再运行一次,然后再添加一下代码运行一次,以次类推,直到所有都输完和正确运行。

输出结果如下:

在这里其实你可以看见两样东西,一样是你的脚本中的打印输出:
c:\>python ex16.py test.txt
We're going to erase 'test.txt'.
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.
line 1: Mary had a little lamb
line 2: It's fleece was white as snow
line 3: It was also tasty
I'm going to write these to the file.
And finally ,we close it.
现在,在你的编辑器中打开你创建的文件(我创建的是 test.txt),并且检查下写入其中的内容,如何,没错吧?

研究训练:

1、如果你感觉不能理解这次练习中的知识的话,返回代码用添加注释的技巧让不理解的地方在你的大脑中放大。给每一行代码加上一个简单的英文注释可以很好的帮助你理解代码或者至少让你知道你需要去搜索什么来帮助自己理解。
2、这里其实有很多代码都是重复的。
3、使用 argv 和read 来写一个和上一次练习类似的脚本来读取你创建的文件。使用了字符串、格式和转义字符来输出line1 ,line2 和 line3其实只要用一个 target.write() 函数就可以代替那里六个 tartget.write()函数。
4、找出为什么使用 open 函数必须传递一个额外的 ‘w'的参数。提示:如果你明确说你只是想要写文件的话,那么加上 ’w' 参数的话可以让 open 函数更安全的打开一个文件。
5、如果你使用了 'w‘参数模式打开一个文件,那么你还需要 target.truncate()函数吗?使用pydoc阅读关于 open 函数的说明看看那样说是对的吗?

学生遇见的常见问题:


truncate() 函数需要带有 ’w' 参数吗?
答:看研究训练5。

‘w’是什么意思?
答:它就是一个字符作为读写文件的一种模式。如果你使用了'w’,那么你的意思就是“用”写“的模式打开一个文本,这就是'w'字符的含义。还有 ‘r'代表”只读“文本,'a’代表追加文本,以及一些修饰符。

我们可以使用什么样的修饰符来修改文件的读写模式?
答:目前你需要知道的一个最重要的修饰符是 ‘+’ 修饰符,所以你可以使用 ‘w+‘ ,’r+' 和 ‘a+’。这些是以读写模式来打开文件 ,根据不同的字符来决定不同的方式来读写文件。

如果使用 open(filename)来打开一个文件是不是默认以 ‘r' (只读)模式呢?
答:是的 ,open 默认以只读模式打开一个文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言实现的程序,实现了Exercise 1至6的要求: ```c #include <stdio.h> #include <stdlib.h> #define MAX_PROCESS_NUM 5 // 最大进程数 #define MAX_TIME_SLICE 5 // 最大运行时间片 // 进程控制块 typedef struct PCB { int pid; // 进程ID int priority; // 优先数 int time_required; // 要求运行时间 int dynamic_priority; // 动态优先数 struct PCB *next; // 指向下一个进程控制块的指针 } PCB; // 初始化进程控制块 void init_pcb(PCB *pcb, int pid, int priority, int time_required) { pcb->pid = pid; pcb->priority = priority; pcb->time_required = time_required; pcb->dynamic_priority = priority; pcb->next = NULL; } // 将进程插入就绪队列 void insert_ready_queue(PCB **head, PCB *pcb) { PCB *p, *q; if (*head == NULL || pcb->dynamic_priority > (*head)->dynamic_priority) { pcb->next = *head; *head = pcb; } else { p = *head; q = p->next; while (q != NULL && pcb->dynamic_priority <= q->dynamic_priority) { p = q; q = q->next; } pcb->next = q; p->next = pcb; } } // 从就绪队列中删除进程 PCB *remove_ready_queue(PCB **head) { PCB *pcb = *head; if (*head != NULL) { *head = (*head)->next; } return pcb; } // 动态地调整进程的优先级和要求运行时间 void adjust_priority_and_time(PCB *pcb) { pcb->dynamic_priority--; pcb->time_required--; if (pcb->dynamic_priority < 0) { pcb->dynamic_priority = 0; } } // 显示就绪队列中的进程 void display_ready_queue(PCB *head) { if (head == NULL) { printf("Ready queue is empty.\n"); return; } printf("Ready queue:"); while (head != NULL) { printf(" P%d(%d,%d,%d) ->", head->pid, head->priority, head->dynamic_priority, head->time_required); head = head->next; } printf(" NULL\n"); } int main() { int i; PCB *pcb[MAX_PROCESS_NUM]; PCB *ready_queue = NULL; char process_name[MAX_PROCESS_NUM][3] = {"P0", "P1", "P2", "P3", "P4"}; // 输入每个进程的优先数和要求运行时间 for (i = 0; i < MAX_PROCESS_NUM; i++) { int priority, time_required; printf("%s:\n", process_name[i]); printf("Priority: "); scanf("%d", &priority); printf("Time required: "); scanf("%d", &time_required); pcb[i] = (PCB *)malloc(sizeof(PCB)); init_pcb(pcb[i], i, priority, time_required); } // 将五个进程按给定的优先数从大到小连成就绪队列 for (i = 0; i < MAX_PROCESS_NUM; i++) { insert_ready_queue(&ready_queue, pcb[i]); } // 处理机调度总是选队首进程运行 while (ready_queue != NULL) { PCB *running_process = remove_ready_queue(&ready_queue); printf("Running process: %s\n", process_name[running_process->pid]); adjust_priority_and_time(running_process); if (running_process->time_required > 0) { insert_ready_queue(&ready_queue, running_process); } else { printf("Process %s is finished.\n", process_name[running_process->pid]); free(running_process); } display_ready_queue(ready_queue); } return 0; } ``` 运行程序后,依次输入五个进程的优先数和要求运行时间,程序会将它们按照优先数从大到小的顺序连成就绪队列,并开始动态优先数算法的调度。程序会输出每次运行的进程名,以及进程控制块的动态变化过程。当某个进程的运行时间为零时,程序会将其状态置为“结束”,且从就绪队列中删除。最后程序结束时,会释放所有进程控制块的内存空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值