ex16.py



 1 # -*- coding:utf-8 -*-
 2
 3 from sys import argv
 4
 5 script, filename = argv  #解包变量参数
 6
 7 print ("we‘re going to erase %r." % filename)
 8 print ("if you don‘t want that, hit CTRL-C(^c).")   #CTRL-C的用法
 9 print ("if you  do want that, hit RETURN.")   # RETURN的用法
10
11 input ("?")
12
13 print ("opening the file...")
14 target = open(filename, ‘w‘)  #‘w‘的意识是‘write’,不可写成大写,否则出现语法错误。本句为定义变量target
15
16 # 学会该如何打开一个其它文件目录的文件?????
17 #‘r‘       open for reading (default)
18 #‘w‘       open for writing, truncating the file first ‘x‘   create a new file and open it for writing
19 #‘a‘       open for writing, appending to the end of the file if it exists
20 #‘b‘       binary mode 二进制
21 #‘t‘       text mode (default) 文本模式
22 #‘+‘       open a disk file for updating (reading and writing)
23 #‘U‘       universal newline mode (deprecated)
24
25 print ("truncating the file .Goodbye!")
26 #target.truncate()     #truncate()的意思是清空文件
27
28 print ("Now I‘m going to ask you for three lines.")
29
30 line1 = input ("line 1: ")
31 line2 = input ("line 2: ")
32 line3 = input ("line 3: ")
33
34 print ("I‘m going to write these to the file.")
35
36 target.write(line1) #write()的意思是写入文件
37 target.write("\n")
38 target.write(line2)
39 target.write("\n")
40 target.write(line3)
41 target.write("\n")
42
43 print ("And finally,we close it.")
44 target.close() # 关闭文件和保存的意思

 
时间: 2024-10-06 16:20:06

ex16.py的相关文章

python练习题-20170916

周末开始第一次尝试对着书写代码练习题 <笨办法学python>--作者Zed A.Shaw,翻译Wang Dingwei ex1.pyprint('hello world')---------------------------------------ex2.py #A comment, this is so you can read your program later.#Anything after the # is ignored by python. print('i could ha

解决这个报错SyntaxError: Non-ASCII character

在文件头部添加这一句即可# -*- coding: utf-8 -* SyntaxError: Non-ASCII character '\xe5' in file ex16.py on line 1, ng declared; see http://python.org/dev/peps/pep-0263/ for details 意思是在文件中存在非ASCII字符(中文): 在头部添加 # -*- coding: utf-8 -*- 或 # -*- coding: cp936 -*-

字打错出现的问题

truncate打成turncate Traceback (most recent call last): File "d:\xuexi\pythonrumen\ex16.py", line 19, in <module> target.turncate()AttributeError: 'file' object has no attribute 'turncate' 原文地址:https://www.cnblogs.com/HL77961/p/9904633.html

os模块,sys模块,json / pickle模块,logging模块

目录 OS模块 sys模块 json和pickle模块 序列化和反序列化 json模块 pickle logging模块 OS模块 能与操作系统交互,控制文件 / 文件夹 # 创建文件夹 import os os.mkdir(r'D:\py_case\test') # 删除文件夹 os.rmdir(r'D:\py_case\test') # # 列出指定目录下所有文件和子目录 (子目录文件不会列出来) res = os.listdir(r'D:\pycharm_project\Test') pr

使用TDD理解views.py与urls.py的关系

首先必须对MVC的概念有初步的认识,django也遵循这样一套规范,views.py相当于视图函数,是整个架构中的处理引擎,而urls.py的作用就是将用户请求送入这样的引擎. 项目结构: urls.py: from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ # Examples: #url(r'^$', 'mysite.views.home', name='home

layers.py cs231n

如果有错误,欢迎指出,不胜感激. import numpy as np def affine_forward(x, w, b): 第一个最简单的 affine_forward简单的前向传递,返回 out,cache """ Computes the forward pass for an affine (fully-connected) layer. The input x has shape (N, d_1, ..., d_k) and contains a minibat

Python pydoc.py

1. 查看帮助,我们可以在python命令行交互环境下用 help函数,比如: 查看 math 模块: >>> help('math')Help on built-in module math: NAME math DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(..

创建py模板

创建模板之后,每次新建py文件,已初始定义的代码段将会自动出现在py文件中.

python下编译py成pyc和pyo

其实很简单,用python -m py_compile file.py python -m py_compile /root/src/{file1,file2}.py编译成pyc文件.也可以写份脚本来做这事:Code: import py_compile py_compile.compile('path') //path是包括.py文件名的路径 用python -O -m py_compile file.py 编译成pyo文件.1.其中的 -m 相当于脚本中的import,这里的-m py_co