714 学习|输入与输出 下

http://docs.pythontab.com/python/python3.4/inputoutput.html#tut-formatting

复习:
问题1: {},(),[] 有什么区别?
Python主要有三种数据类型:字典、列表、元组。其分别由花括号,中括号,小括号表示。
如:
字典:dic={'a':12,'b':34}
列表:list=[1,2,3,4]
元组:tup=(1,2,3,4)

问题2: python中 ** 有什么用?

两个乘号就是乘方,比如24,结果就是2的4次方,结果是16
一个乘号,如果操作数是两个数字,就是这两个数字相乘,如24,结果为8
如果是字符串、列表、元组与一个整数N相乘,返回一个其所有元素重复N次的同类型对象,比如"str"3将返回字符串"strstrstr"

如果是函数定义中参数前的表示的是将调用时的多个参数放入元组中,则表示将调用函数时的关键字参数放入一个字典中
如定义以下函数
def func(
args):print(args)
当用func(1,2,3)调用函数时,参数args就是元组(1,2,3)
定义以下函数

def func(**args):print(args)
当用func(a=1,b=2)调用函数时,参数args将会是字典{'a':1,'b':2}

如果是在函数调用中,args表示将可迭代对象扩展为函数的参数列表
args=(1,2,3)
func=(
args)
等价于函数调用func(1,2,3)
函数调用的表示将字典扩展为关键字参数
args={'a':1,'b':2}
func(
args)
等价于函数调用 func(a=1,b=2)

python的路径操作

1.获得当前路径 在Python中可以使用os.getcwd()函数获得当前的路径。
os.getcwd() 该函数不需要传递参数,它返回当前的目录。需要说明的是,当前目录并不是指脚本所在的目录,而是所运行脚本的目录。

2.获得目录中的内容 在Python中可以使用os.listdir()函数获得指定目录中的内容。其原型如下所示。 os.listdir(path) 其参数含义如下。 · path 要获得内容目录的路径。 以下实例获得当前目录的内容。

import os
os.listdir(os.getcwd()) # 获得当前目录中的内容 ['dde.pyd', 'license.txt', 'Pythonwin.exe', 'scintilla.dll', 'win32ui.pyd', 'win32uiole.pyd', 'pywin']
练习:
os.listdir('temp') #获得子目录“temp”中的文件信息

3.创建目录 在Python中可以使用os.mkdir()函数创建目录。 os.mkdir(path) 其参数含义为。 · path 要创建目录的路径。 以下的实例将在users/book目录下创建temp目录。

import os
os.mkdir('uses/book/temp') # 使用os.mkdir创建目录

4.删除目录 在Python中可以使用os.rmdir()函数删除目录。os.rmdir(path) 其参数含义如下。 · path 要删除的目录的路径。

import os
os.rmdir('users/book/temp') # 删除目录 需要说明的是,使用os.rmdir删除的目录必须为空目录,否则函数出错。

5.判断是否是目录 在Python中可以使用os.path.isdir()函数判断某一路径是否为目录。 os.path.isdir(path) 其参数含义如下。 · path 要进行判断的路径。 以下实例判断E:/book/temp是否为目录。

import os
os.path.isdir('E:\book\temp') # 判断E:\book\temp是否为目录 True # 表E:\book\temp是目录

6.判断是否为文件 在Python中可以使用os.path.isfile()函数判断某一路径是否为文件。其函数原型如下所示。 os.path.isfile(path) 其参数含义如下。 · path:要进行判断的路径。 以下实例判断E:\book\temp是否为文件。 >>> import os >>> os.path.isfile('E:\book\temp') # 判断是否为文件 False # 表示E:\book\temp不是文件

练习:

可以用 ‘**’ 标志将这个字典以关键字参数的方式传入。

table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
这种方式与新的内置函数 vars() 组合使用非常有效。该函数返回包含所有局部变量的字典。

要进一步了解字符串格式化方法 str.format() ,参见 formatstrings 。

7.1.1. 旧式的字符串格式化

7.2. 文件读写

import.os
File "<stdin>", line 1
import.os
^
SyntaxError: invalid syntax
import os
os.getcwd()
'/Users/Jane/Desktop/Python 学习'
f=open('temp/7-2.txt','r')
f
<_io.TextIOWrapper name='temp/7-2.txt' mode='r' encoding='UTF-8'>
os.listdir(/temp)
File "<stdin>", line 1
os.listdir(/temp)
^
SyntaxError: invalid syntax
os.listdir('\temp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '\temp'
os.listdir('\temp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '\temp'
os.listdir(os.getcwd())
['.DS_Store', '714', 'temp']
os.listdir('temp')
['7-2.txt']
os.mkdir('/users/Jane/Desktop/test')
os.rmdir('/users/Jane/Desktop/test')
os.path.isdir('temp')
True
os.path.isfile('temp')
False
os.path.isfile('7-2')
False
f=open('temp/7-2.txt','r')
f
<_io.TextIOWrapper name='temp/7-2.txt' mode='r' encoding='UTF-8'>
f=seek(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seek' is not defined
f.seek(0)
0
f.readline()
'This is the first line.\n'
f.readline()
'This is the second line.'
f.readlines()
[]
f.seek(0)
0
f.readlines()
['This is the first line.\n', 'This is the second line.']
f.write('This is a test\n')

os.getcwd()
'/Users/Jane/Desktop/Python 学习'
>>> f=open('temp/7-2.txt','r')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='r' encoding='UTF-8'>
>>> os.listdir(/temp)
File "<stdin>", line 1
os.listdir(/temp)
^
SyntaxError: invalid syntax
>>> os.listdir('\temp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '\temp'
>>> os.listdir('\temp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '\temp'
>>> os.listdir(os.getcwd())
['.DS_Store', '714', 'temp']
>>> os.listdir('temp')
['7-2.txt']
>>> os.mkdir('/users/Jane/Desktop/test')
>>> os.rmdir('/users/Jane/Desktop/test')
>>> os.path.isdir('temp')
True
>>> os.path.isfile('temp')
False
>>> os.path.isfile('7-2')
False
>>> f=open('temp/7-2.txt','r')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='r' encoding='UTF-8'>
>>> f=seek(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seek' is not defined
>>> f.seek(0)
0
>>> f.readline()
'This is the first line.\n'
>>> f.readline()
'This is the second line.'
>>> f.readlines()
[]
>>> f.seek(0)
0
>>> f.readlines()
['This is the first line.\n', 'This is the second line.']
>>> f.write('This is a test\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
f=open('temp/7-2.txt','w')
f
<_io.TextIOWrapper name='temp/7-2.txt' mode='w' encoding='UTF-8'>
f.write('This is a test\n')
15
f=open('/temp/7-2.txt','rb+')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/temp/7-2.txt'
f=open('temp/7-2.txt','rb+')
f
<_io.BufferedRandom name='temp/7-2.txt'>
f.write(b'0123456789abcdef')
16
f.seek(5)
5
f.seek()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
os.listdir('temp')
['7-2.txt']
>>> os.mkdir('/users/Jane/Desktop/test')
>>> os.rmdir('/users/Jane/Desktop/test')
>>> os.path.isdir('temp')
True
>>> os.path.isfile('temp')
False
>>> os.path.isfile('7-2')
False
>>> f=open('temp/7-2.txt','r')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='r' encoding='UTF-8'>
>>> f=seek(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seek' is not defined
>>> f.seek(0)
0
>>> f.readline()
'This is the first line.\n'
>>> f.readline()
'This is the second line.'
>>> f.readlines()
[]
>>> f.seek(0)
0
>>> f.readlines()
['This is the first line.\n', 'This is the second line.']
>>> f.write('This is a test\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> f=open('temp/7-2.txt','w')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='w' encoding='UTF-8'>
>>> f.write('This is a test\n')
15
>>> f=open('/temp/7-2.txt','rb+')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/temp/7-2.txt'
>>> f=open('temp/7-2.txt','rb+')
>>> f
<_io.BufferedRandom name='temp/7-2.txt'>
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)
5
>>> f.seek()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: seek() takes at least 1 argument (0 given)
f.read(1)
b'5'

f.seek(-3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
f.seek(-3,2)

f=open('temp/7-2.txt','r')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='r' encoding='UTF-8'>
>>> f=seek(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seek' is not defined
>>> f.seek(0)
0
>>> f.readline()
'This is the first line.\n'
>>> f.readline()
'This is the second line.'
>>> f.readlines()
[]
>>> f.seek(0)
0
>>> f.readlines()
['This is the first line.\n', 'This is the second line.']
>>> f.write('This is a test\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> f=open('temp/7-2.txt','w')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='w' encoding='UTF-8'>
>>> f.write('This is a test\n')
15
>>> f=open('/temp/7-2.txt','rb+')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/temp/7-2.txt'
>>> f=open('temp/7-2.txt','rb+')
>>> f
<_io.BufferedRandom name='temp/7-2.txt'>
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)
5
>>> f.seek()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: seek() takes at least 1 argument (0 given)
>>> f.read(1)
b'5'
>>> f.seek(-3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> f.seek(-3,2)
13

f.close()
with open('temp/7-2.txt','r') as f:
... read_data = f.read()
... f.closed

>>> f=seek(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seek' is not defined
>>> f.seek(0)
0
>>> f.readline()
'This is the first line.\n'
>>> f.readline()
'This is the second line.'
>>> f.readlines()
[]
>>> f.seek(0)
0
>>> f.readlines()
['This is the first line.\n', 'This is the second line.']
>>> f.write('This is a test\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> f=open('temp/7-2.txt','w')
>>> f
<_io.TextIOWrapper name='temp/7-2.txt' mode='w' encoding='UTF-8'>
>>> f.write('This is a test\n')
15
>>> f=open('/temp/7-2.txt','rb+')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/temp/7-2.txt'
>>> f=open('temp/7-2.txt','rb+')
>>> f
<_io.BufferedRandom name='temp/7-2.txt'>
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)
5
>>> f.seek()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: seek() takes at least 1 argument (0 given)
>>> f.read(1)
b'5'
>>> f.seek(-3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> f.seek(-3,2)
13
>>> f.close()
>>> with open('temp/7-2.txt','r') as f:
... read_data = f.read()
... f.closed
File "<stdin>", line 3
f.closed
^
SyntaxError: invalid syntax
print read_data
File "<stdin>", line 1
print read_data
^
SyntaxError: Missing parentheses in call to 'print'
with open('temp/7-2.txt','r') as f:
... print(f.read())
...
0123456789abcdef
f.closed
True

问题能不能明确一点,一个问题已经给了八个答案了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容