235 lines
8.0 KiB
Python
235 lines
8.0 KiB
Python
"""
|
||
=================================
|
||
Author: Flora Chen
|
||
Time: 2020/2/25 19:27
|
||
-_- -_- -_- -_- -_- -_- -_- -_-
|
||
=================================
|
||
"""
|
||
|
||
"""
|
||
1、实现一个文件复制器函数,通过给函数传入一个路径,复制该路径下面所有的文件(目录不用复制)到当前目录,
|
||
|
||
提示:os模块结合文件读写操作 、即可实现
|
||
|
||
步骤提示:获取指定路径下的所有文件信息,判断是否是文件,是文件则进行复制(读取内容,写入到新文件)
|
||
"""
|
||
|
||
import os
|
||
|
||
|
||
# ------------------方法1--------------------
|
||
# 复制指定路径下的文件到当前文件所在的目录
|
||
def file_copy(path):
|
||
# 如果传入的是个目录就进行遍历文件复制
|
||
if os.path.isdir(path):
|
||
for file in os.listdir(path):
|
||
# 定位目标目录的文件
|
||
file_path = os.path.join(path, file)
|
||
|
||
# 如果是文件,则进行文件复制
|
||
if os.path.isfile(file_path):
|
||
# 读取文件的内容
|
||
with open(file_path, 'rb') as f:
|
||
content = f.read()
|
||
|
||
# 将上面读取到的内容写入到新文件中, 新文件跟当前文件同级,并且命名带有cp
|
||
# with open(os.path.join(os.getcwd(), 'cp' + file), 'wb') as f:
|
||
with open('cp' + file, 'wb') as f:
|
||
f.write(content)
|
||
|
||
# 如果是文件的话,则直接复制
|
||
elif os.path.isfile(path):
|
||
# 读取文件的内容
|
||
with open(path, 'rb') as f:
|
||
content = f.read()
|
||
|
||
# 将上面读取到的内容写入到新文件中, 新文件跟当前文件同级,并且命名带有cp
|
||
# with open(os.path.join(os.getcwd(), 'cp' + file), 'wb') as f:
|
||
with open('cp' + path, 'wb') as f:
|
||
f.write(content)
|
||
|
||
|
||
|
||
# 需要复制的文件所在的目录
|
||
target_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Pack')
|
||
|
||
file_copy(target_path)
|
||
|
||
|
||
# 复制当前文件所在目录下的所有文件到指定目录
|
||
# def files_copy(directory_name, path):
|
||
# # 判断当前工作路径下是否存在文件
|
||
# for file in os.listdir(path):
|
||
# # 在当前工作目录下面创建一个与当前文件同级的目录来保存复制的文件。不存在则创建。
|
||
# if directory_name not in os.listdir(path):
|
||
# os.mkdir(directory_name)
|
||
#
|
||
# # 如果是文件,则进行文件复制
|
||
# if os.path.isfile(file):
|
||
# # 在新目录下新建文件,读取当前工作目录下文件的内容
|
||
# with open(file, 'rb') as f:
|
||
# content = f.read()
|
||
#
|
||
# # 将上面读取到的内容写入到新文件中
|
||
# with open(os.path.join(directory_name, 'cp' + file), 'wb') as f:
|
||
# f.write(content)
|
||
|
||
# files_copy('TestDirectory', os.getcwd())
|
||
|
||
|
||
|
||
|
||
# ------------------方法2--------------------
|
||
# def copy_file(file):
|
||
# # 获取文件名以及后缀
|
||
# dest_file = 'cp' + os.path.basename(file)
|
||
#
|
||
# # 读取文件内容
|
||
# with open(file, 'rb') as f:
|
||
# content = f.read()
|
||
#
|
||
# # 在新文件中写入内容
|
||
# with open(dest_file, 'wb') as f:
|
||
# f.write(content)
|
||
#
|
||
#
|
||
# def is_file(path):
|
||
# # 获取当前目录下的所有文件
|
||
# for file in os.listdir(path):
|
||
# # 获取所有文件的绝对路径
|
||
# file_path = os.path.join(path, file)
|
||
#
|
||
# # 判断是否是文件,是文件就调用copy_file
|
||
# if os.path.isfile(file_path):
|
||
# copy_file(file_path)
|
||
#
|
||
#
|
||
# is_file(target_path)
|
||
|
||
"""
|
||
case.txt文件,里面中存储了很多用例数据: 如下,每一行数据就是一条用例数据,
|
||
# 文件中数据(可以先直接复制到文件中)
|
||
url:www.baidu.com,mobilephone:13760246701,pwd:123456
|
||
url:www.baidu.com,mobilephone:15678934551,pwd:234555
|
||
url:www.baidu.com,mobilephone:15678934551,pwd:234555
|
||
url:www.baidu.com,mobilephone:15678934551,pwd:234555
|
||
url:www.baidu.com,mobilephone:15678934551,pwd:234555
|
||
|
||
# 要求: 请把这些数据读取出来,转换为列表的格式:如下
|
||
[{'url': 'www.baidu.com', 'mobilephone': '13760246701', 'pwd': '123456'}, {'url': 'www.baidu.com', 'mobilephone': '15678934551', 'pwd': '234555'},{'url': 'www.baidu.com', 'mobilephone': '15678934551', 'pwd': '234555'},{'url': 'www.baidu.com', 'mobilephone': '15678934551', 'pwd': '234555'},
|
||
{'url': 'www.baidu.com', 'mobilephone': '15678934551', 'pwd': '234555'}]
|
||
|
||
# 提示:可以分析读取出来的每一行字符串中的内容,然后使用的字符串分割方法进行分割,想办法组装成字典。
|
||
# 注意点:数据中如果有换行符'\n',要想办法去掉
|
||
"""
|
||
|
||
|
||
# ------------------方法1--------------------
|
||
def read_data(file):
|
||
# 定义3个空列表来接收数据
|
||
key = []
|
||
value = []
|
||
case = []
|
||
# 读取文件中的内容
|
||
with open(file, 'r', encoding='utf-8') as f:
|
||
# 将读取的每一行内容使用‘,’分割
|
||
for i in f.readlines():
|
||
res = i.split(',')
|
||
# 遍历分割后的列表,将元素的值分别追加到列表key和value中
|
||
for j in res:
|
||
res2 = j.split(':')
|
||
key.append(res2[0])
|
||
value.append(res2[1].replace('\n', ''))
|
||
# 将两个新列表打包成字典并追加到列表case中
|
||
case.append(dict(zip(key, value)))
|
||
return case
|
||
|
||
|
||
print(read_data('case.txt'))
|
||
|
||
|
||
# ------------------方法2--------------------
|
||
def analyse(file):
|
||
# 定义一个空列表来接收数据
|
||
case_list = []
|
||
|
||
# 打开文件
|
||
with open(file, 'r') as f:
|
||
# 按行读取文件中所有的内容
|
||
for line in f.readlines():
|
||
# 定义一个空字典来接收每一行的数据
|
||
case = {}
|
||
# 将每行内容使用逗号分隔
|
||
for sub in line.split(','):
|
||
# 再将列表中的每个元素用冒号分割
|
||
flied = sub.split(':')
|
||
|
||
# 如果有换行,则去除;并且# 给字典新增键值对
|
||
if flied[1].endswith('\n'):
|
||
case[flied[0]] = flied[1][:-1]
|
||
else:
|
||
case[flied[0]] = flied[1]
|
||
# 三目运算符 等同于上面的代码
|
||
# value[flied[0]] = flied[1][:-1]if flied[1].endswith('\n') else flied[1]
|
||
|
||
# 将得到的字典追加到列表中
|
||
case_list.append(case)
|
||
return case_list
|
||
|
||
|
||
print(analyse('case.txt'))
|
||
|
||
"""
|
||
3、第三题:练习模块导入的方式(不用提交)
|
||
Pass
|
||
"""
|
||
|
||
"""
|
||
4、整理笔记
|
||
"""
|
||
|
||
"""
|
||
5、编程逻辑扩展练习题(不用提交)
|
||
(1)有一个猴子,第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半 在加一个。到第10天早上想再吃时,见只剩下一个桃子了。
|
||
请通过一段通过代码来计算 第一天摘了多少个桃子?
|
||
|
||
|
||
(2)题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?
|
||
"""
|
||
|
||
|
||
def monkey(days):
|
||
peach = 1
|
||
for day in range(1, days):
|
||
peach = (peach + 1) * 2
|
||
print('小猴子第1天总共摘了{}桃子'.format(peach))
|
||
return peach
|
||
print(monkey(10))
|
||
|
||
|
||
# def monkey(days):
|
||
# peach = 1534
|
||
# for day in range(1, days + 1):
|
||
# eat_peach = peach / 2 + 1
|
||
# peach = peach - eat_peach
|
||
# print('小猴子第{}天总共吃了{}桃子, 剩余{}'.format(day, eat_peach, peach))
|
||
# return peach
|
||
#
|
||
#
|
||
# print(monkey(10))
|
||
|
||
|
||
def ball(height, times):
|
||
count_height = 0
|
||
for count in range(1, times + 1):
|
||
if count == 1:
|
||
count_height = height
|
||
else:
|
||
count_height = count_height + height * 2
|
||
height = height / 2
|
||
print('第{}次落地剩余{}米, 总共经过{}米'.format(count, height, count_height))
|
||
return count_height
|
||
|
||
|
||
print(ball(100, 10))
|