复制的python代码格式错误_记录学习python第4天-文件复制/OS模块/异常

今天开始面向google和stackoverflow编程啦,顺便重新拾起我的VOE和扇贝。

笔记依旧更新在石墨文档。

Python基础知识 《Python基础知识》,可复制链接后用石墨文档 App 或小程序打开

算法题笔记

python算法题 《python算法题》,可复制链接后用石墨文档 App 或小程序打开

2.4 文件的复制和OS模块

OS模块

os.path:对系统里面的东西进行修改

os.path.dirname(__file__):表示当前文件的目录

os.listdir(): 返回指定目录下的所有的文件和文件夹

os.mkdir():当前目录下创建文件夹

os.exists(path):当前目录下是否存在该文件

os.remove():删除文件

os.rmdir():只能删除空的文件夹

例子:删除文件夹以及文件夹下面的文件

import os

path = r'C:\Users\a1437\Desktop\testpy\BiliBili_learning\log'

filelist = os.listdir(path)

for file in filelist:

# 还需要拼接文件名

path1 = os.path.join(path,file)

os.remove(path1)

else:

os.rmdir(path)相对路径:

绝对路径:os.path.isabs('路径'),判断是否是绝对路径

os.path.abspath(__file__),获取当前文件的绝对路径

文件复制

# with 结合 open 使用,可以帮助我们自动资源

with open(r'test.txt', 'rb') as stream:

container = stream.read() # 读取文件内容

with open(r'test1.txt', 'wb') as wstream:

wstream.write(container)

print('copy completely!')

文件复制

# 文件的复制

import os

src_path = r'C:\xxxxx\log'

target_path = r'C:\xxxxxx\log2'

# 封装成函数

def copy(src, target):

if os.path.isdir(src) and os.path.isdir(target):

filelist = os.listdir(src)

for file in filelist:

path = os.path.join(src, file)

with open(path) as rstream:

container = rstream.read()

path1 = os.path.join(target, file)

# 注意open不能打开文件夹,定位到一个文件名

with open(path1,'w') as wstream:

wstream.write(container)

else:

print('复制完毕!')

copy(src_path,target_pat

文件复制【递归方式判断文件夹】

# 文件的复制

import os

src_path = r'C:\Users\a1437\Desktop\testpy\BiliBili_learning\log'

target_path = r'C:\Users\a1437\Desktop\testpy\BiliBili_learning\log2'

# 封装成函数

def copy(src, target):

filelist = os.listdir(src)

for file in filelist:

path = os.path.join(src, file)

# 判断是否为文件夹

if os.path.isdir(path):

new_target = os.path.join(target, file)

os.mkdir(new_target)

copy(path, new_target)

else:

with open(path) as rstream:

container = rstream.read()

path1 = os.path.join(target, file)

# 注意open不能打开文件夹,定位到一个文件名

with open(path1,'w') as wstream:

wstream.write(container)

else:

print('复制完成!')

copy(src_path,target_pat

2.5 总结小案例:

【图书管理系统/登录/注册/浏览】

import os

# 登录

def login():

username = input('请输入你的用户名:')

password = input('请输入你的密码:')

if username and password:

with open(r'library\userinfo.text') as rstream:

while True:

user = rstream.readline()

if not user:

print('用户名或者密码错误!')

break

input_user = '{} {}\n'.format(username, password)

if user == input_user:

print('登录成功!')

break

# 注册

def register():

username = input('请输入你的用户名:')

password = input('请输入你的密码:')

repassword = input('请再次输入你的密码:')

if password == repassword:

# 追加写入

with open(r'library/userinfo.text','a') as wstream:

wstream.write('{} {}\n'.format(username, password))

print('用户注册成功!')

else:

print('密码不一致!')

# 遍历图书

def listBook():

with open(r'library/book.txt', encoding='utf-8') as rstream:

container = rstream.readlines()

for bookname in container:

print(bookna

3.异常程序运行异常

3.1 异常处理

# 异常处理

'''

try:

可能出现的异常

except:

如果有异常执行的代码

finally:

无论是否存在异常都会被执行的代码

'''

3.2 异常捕捉

except Exception:

Exception一般是大多数异常的父类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值