os 获取文件目录 dirname,filename
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import sys
import os # --- 多种操作系统接口
# 导入的依赖特定操作系统的模块的名称。以下名称目前已注册: 'posix', 'nt', 'java'.
# POSIX 一般指可移植操作系统接口
# Windows os.name 返回 'nt'
# linux or mac 返回 'posix'
print(os.name)
# /Users/root/project/new-oms
print(os.getcwd()) # 获取当前工作目录路径
# /Users/root/project/new-oms
print(os.path.abspath('.')) # 获取当前工作目录路径
# /Users/root/project/new-oms/test.txt
print(os.path.abspath('test.txt')) # 获取当前目录文件下的工作目录路径
# /Users/root/project
print(os.path.abspath('..')) # 获取当前工作的父目录 !注意是父目录路径
# /Users/root/project/new-oms
print(os.path.abspath(os.curdir)) # 获取当前工作目录路径
# ./test/index.py
print(sys.argv[0])
# 当前目录的绝对路径
# /Users/root/project/new-oms/./test/index.py
__filename__ = os.path.join(os.getcwd(), sys.argv[0])
print(__filename__)
# /Users/root/project/new-oms/test/index.py
print(os.path.abspath(__filename__))
# _file__是当前执行的文件
# 获取当前文件__file__的路径,__filename__ = /Users/root/project/new-oms/test/index.py
print("__filename__=%s" % (os.path.realpath(__file__)))
# 获取当前文件__file__的所在目录,__dirname__ = /Users/root/project/new-oms/test
print("__dirname__=%s" % (os.path.dirname(os.path.realpath(__file__))))
# 获取当前文件__file__的所在目录,__dirname__ = /Users/root/project/new-oms/test
print("__dirname__=%s" % (os.path.split(os.path.realpath(__file__))[0]))
print(__file__) # ./test/index.py
print(os.uname())
# sysname - 操作系统名
# nodename - 机器在网络上的名称(需要先设定)
# release - 操作系统发行信息
# version - 操作系统版本信息
# machine - 硬件标识符
# posix.uname_result(sysname='Darwin', nodename='rootMacBook-Air.local', release='20.6.0',
# version='Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:27 PDT 2021; root:xnu-7195.141.2~5/RELEASE_ARM64_T8101', machine='arm64')
爬虫小实践
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
__author__ = '27510299@qq.com'
import urllib.request
def clear():
'''该函数用于清屏'''
print('内容较多,显示3秒后翻页')
time.sleep(3)
OS = platform.system()
if (OS == 'Window'):
os.system('cls')
else:
os.system('clear')
def linkHibobi():
url = 'https://www.baidu.com'
try:
response = urllib.request.urlopen(url, timeout=3)
result = response.read().decode('utf-8')
except Exception as e:
print("网络地址错误")
exit()
with open('hibobi.txt', 'w', encoding='utf8') as fp:
fp.write(result)
print("获取url信息:response.geturl(): %s" %response.geturl())
print("获取返回代码:response.getcode(): %s" %response.getcode())
print("获取返回信息:response.info(): %s" %response.info())
if __name__ == '__main__':
linkHibobi()
2.爬虫小实践
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
__author__ = '27510299@qq.com'
import urllib.request
import sys
import re
def testArgument():
'''测试输入参数,只需要一个参数'''
if len(sys.argv) != 2:
print("只能有一个参数")
tipUp()
exit()
else:
TP = TestProxy(sys.argv[1])
def tipUp():
'''显示提示信息'''
class TestProxy():
'''这个类测试proxy是否有效'''
def clear():
'''该函数用于清屏'''
print('内容较多,显示3秒后翻页')
time.sleep(3)
OS = platform.system()
if (OS == 'Window'):
os.system('cls')
else:
os.system('clear')
def linkHibobi():
url = 'https://www.baidu.com'
try:
response = urllib.request.urlopen(url, timeout=3)
result = response.read().decode('utf-8')
except Exception as e:
print("网络地址错误")
exit()
with open('hibobi.txt', 'w', encoding='utf8') as fp:
fp.write(result)
print("获取url信息:response.geturl(): %s" %response.geturl())
print("获取返回代码:response.getcode(): %s" %response.getcode())
print("获取返回信息:response.info(): %s" %response.info())
if __name__ == '__main__':
linkHibobi()