欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > python常用语法笔记(持续更新)

python常用语法笔记(持续更新)

2025/5/7 12:19:32 来源:https://blog.csdn.net/A_art_xiang/article/details/143476177  浏览:    关键词:python常用语法笔记(持续更新)

文章目录

  • 一、基础语法
    • 1、sleep休眠
    • 2、os系统操作
      • (1)获取环境变量
      • (2)os.path操作
    • 3、文件操作
      • (1)文件读取模式详解
      • (2)逐行读取文件
      • (3)逐行写入文件
    • 4、字符串操作
      • (1)字符串转义
    • 5、时间操作
      • (1)获取当前时间
      • (2)时间格式化

一、基础语法

1、sleep休眠

import time# 休眠5秒钟,可以写小数
time.sleep(5)

2、os系统操作

(1)获取环境变量

import os
# 第一个参数为key,第二个参数为如果没有该key的默认值
name = os.getenv("MY_NAME", "World")
print(f"Hello {name} from Python")

(2)os.path操作

import ospath_temp = 'E:\test.txt'
# 判断是否是目录
print(os.path.isdir(path_temp))
# 判断是否是文件
print(os.path.isfile(path_temp))
# 判断文件是否存在,包括文件和目录
print(os.path.exists(path_temp))# 获取当前脚本的绝对路径
script_path = os.path.abspath(__file__)
# 获取文件所在的目录
project_path = os.path.dirname(script_path)

3、文件操作

(1)文件读取模式详解

在这里插入图片描述

(2)逐行读取文件

file_path = 'E:\phone.txt'file_content= []
# 逐行读取文件
with open(file_path, 'r') as file:for line in file:# 处理每一行数据,去除换行符file_content.append(line.strip())print(file_content)

(3)逐行写入文件

# 假设我们有一个大文件file.txt,我们要向其中追加文本
filename = "file.txt"# 要追加的文本行
lines_to_append = ["这是第一行\n", "这是第二行\n", "这是第三行\n"]# 使用with语句确保文件正确打开和关闭
with open(filename, "w") as file:for line in lines_to_append:file.write(line)

4、字符串操作

(1)字符串转义

# 如果希望反斜杠被解释为普通字符,你可以在字符串前面添加一个 r,表示这是一个原始字符串。例如:r"E:\photo/naked_wife.jpg"
image = cv.imread(r"E:\photo/naked_wife.jpg")

5、时间操作

(1)获取当前时间

import datetime
# 获取当前时间
current_time = datetime.datetime.now()
# 输出当前时间 2024-11-21 06:23:54.802026
print(current_time)
# 分别获取当前年、月、日、时、分、秒
current_year = current_datetime.year
current_month = current_datetime.month
current_day = current_datetime.day
current_hour = current_datetime.hour
current_minute = current_datetime.minute
current_second = current_datetime.second
import time
# 获取当前时间戳
timestamp = time.time()
# 转换为可读性更好的格式 Thu Nov 21 06:24:23 2024
current_time = time.ctime(timestamp)
import time# 获取当前时间的结构化元组
current_time_struct = time.localtime()# 分别获取当前年、月、日、时、分、秒
current_year = current_time_struct.tm_year
current_month = current_time_struct.tm_mon
current_day = current_time_struct.tm_mday
current_hour = current_time_struct.tm_hour
current_minute = current_time_struct.tm_min
current_second = current_time_struct.tm_sec# 输出当前时间 time.struct_time(tm_year=2024, tm_mon=11, tm_mday=21, tm_hour=9, tm_min=25, tm_sec=44, tm_wday=3, tm_yday=326, tm_isdst=0)
print(current_time_struct)

(2)时间格式化

import datetime
# 获取当前时间
now = datetime.datetime.now()
# 格式化当前时间
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
print(now_str)import time
# 获取当前时间戳
timestamp = time.time()
# 格式化时间戳为字符串
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
print(formatted_time)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词