Python学习——文件操作和异常处理

# title = "Alice in Wonderland"# print(title.split())

def count_words(filename):    ‘‘‘ count how many words in a text file ‘‘‘    try:        with open(filename) as f_obj:            contents = f_obj.read()    except FileNotFoundError:        pass        #msg = "Sorry, the file " + filename + " does not exist."        #print(msg)    else:        words = contents.split()        print("The file " + filename + " has about " + str(len(words)) + " words.")

def addTwoNumber():    ‘‘‘ add two numbers. ‘‘‘    first_number = input("First Number: ")    second_number = input("Second Number: ")    try:        first_number = int(first_number)        second_number = int(second_number)    except ValueError:        print("Please check if your input is legal.")        return False    else:        res = first_number + second_number        print(str(first_number)+" + "+str(second_number)+" is "+str(res))        return True

原文地址:https://www.cnblogs.com/QiLF/p/9231787.html

时间: 2024-08-28 06:17:23

Python学习——文件操作和异常处理的相关文章

Python学习—文件操作

1.文件基础知识 1.文件是存储在外部介质上的数据的集合,文件的基本单位是字节,文件所含的字节数就是文件的长度.每个字节都有一个默认的位置,位置从0开始,文件头的位置就是0,文件尾的位置是文件内容结束后的后一个位置,该位置上没有文件内容,为空.文件的读写操作从文件指针所在的位置开始,即读会从文件指针所在的位置开始读取,写会从文件指针所在的位置开始写,如有内容,则会被覆盖.2.按文件中数据的组织形式把文件分为文本文件和二进制文件两类.文本文件存储的是常规字符串,由文本行组成,通常以换行符'\n'结

Python学习-文件操作

打开和关闭文件: Python 提供了必要的函数和方法进行默认情况下的文件基本操作. open 函数 你必须先用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写. 语法: file object = open(file_name [, access_mode][, buffering]) 各个参数的细节如下: ①file_name:file_name变量是一个包含了你要访问的文件名称的字符串值. ②access_mode:access_mode

python之文件操作-复制、剪切、删除等

下面是把sourceDir文件夹下的以.JPG结尾的文件全部复制到targetDir文件夹下: <span style="font-size:18px;">>>>import os >>> import os.path >>> import shutil >>> def copyFiles(sourceDir,targetDir): for files in os.listdir(sourceDir):

python学习之操作mysql

欢迎点击个人博客 http://www.iwangzheng.com/ 刚开始学python,所以很多代码都需要在ipython里尝试一下.今天记录的是最基本的操作mysql数据库. 写数据库连接操作的时候,仿佛回到了当年在前两家公司写asp.net的感觉. 1.首先在mysql数据库里新建个数据库 create database db_02 default charset utf8; create table user (id int auto_increment primary key,us

Python开发【第三章】:Python的文件操作

Python的文件操作 一.读取操作,3种读取方式的区别 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian info_file = open("here_we_are",encoding="utf-8") #默认读取模式 print(info_file) #不加参数,直接打印 #<_io.TextIOWrapper name='here_we_are' mode='r' encoding='u

python中文件操作的其他方法

前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r',encoding='utf-8')for i in p:print(i)结果如下: hello,everyone白日依山尽,黄河入海流.欲穷千里目,更上一层楼. 1.readline   #读取一行内容 p=open('poems','r',encoding='utf-8') print(p.rea

python学习笔记 操作文件和目录

如果我们要操作文件.目录,可以在命令行下面输入操作系统提供的各种命令来完成.比如dir.cp等命令. 如果要在Python程序中执行这些目录和文件的操作怎么办?其实操作系统提供的命令只是简单地调用了操作系统提供的接口函数,Python内置的os模块也可以直接调用操作系统提供的接口函数. 打开Python交互式命令行,我们来看看如何使用os模块的基本功能: >>> import os >>> os.name # 操作系统类型 'posix' 如果是posix,说明系统是L

python文件操作和异常处理

一.文件操作 详细点击:http://www.cnblogs.com/linhaifeng/articles/5984922.html 二.异常处理: 详细点击:http://www.cnblogs.com/linhaifeng/articles/6232220.html 原文地址:https://www.cnblogs.com/pantong/p/10503064.html

[python拾遗]文件操作

文件操作 1.open()函数 open()函数主要用于文件处理,一般分为下面3个过程: 1.打开文件 2.操作文件 3.关闭文件 常见的格式示例: f = open('note.txt','r') f.read() f.close() 1.打开文件 文件句柄 = open('文件路径','模式') 常见的模式有: 1.‘r’,只读 2.‘w’,只写(当对打开执行只写操作后,文件原内容将会被清空,注意备份) 3.‘a’,追加 "+" 表示可以同时读写某个文件 1.‘r+’ 2.‘w+’