os 获取文件目录 dirnamefilename

  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. import os # --- 多种操作系统接口
  5. # 导入的依赖特定操作系统的模块的名称。以下名称目前已注册: 'posix', 'nt', 'java'.
  6. # POSIX 一般指可移植操作系统接口
  7. # Windows os.name 返回 'nt'
  8. # linux or mac 返回 'posix'
  9. print(os.name)
  10. # /Users/root/project/new-oms
  11. print(os.getcwd()) # 获取当前工作目录路径
  12. # /Users/root/project/new-oms
  13. print(os.path.abspath('.')) # 获取当前工作目录路径
  14. # /Users/root/project/new-oms/test.txt
  15. print(os.path.abspath('test.txt')) # 获取当前目录文件下的工作目录路径
  16. # /Users/root/project
  17. print(os.path.abspath('..')) # 获取当前工作的父目录 !注意是父目录路径
  18. # /Users/root/project/new-oms
  19. print(os.path.abspath(os.curdir)) # 获取当前工作目录路径
  20. # ./test/index.py
  21. print(sys.argv[0])
  22. # 当前目录的绝对路径
  23. # /Users/root/project/new-oms/./test/index.py
  24. __filename__ = os.path.join(os.getcwd(), sys.argv[0])
  25. print(__filename__)
  26. # /Users/root/project/new-oms/test/index.py
  27. print(os.path.abspath(__filename__))
  28. # _file__是当前执行的文件
  29. # 获取当前文件__file__的路径,__filename__ = /Users/root/project/new-oms/test/index.py
  30. print("__filename__=%s" % (os.path.realpath(__file__)))
  31. # 获取当前文件__file__的所在目录,__dirname__ = /Users/root/project/new-oms/test
  32. print("__dirname__=%s" % (os.path.dirname(os.path.realpath(__file__))))
  33. # 获取当前文件__file__的所在目录,__dirname__ = /Users/root/project/new-oms/test
  34. print("__dirname__=%s" % (os.path.split(os.path.realpath(__file__))[0]))
  35. print(__file__) # ./test/index.py
  36. print(os.uname())
  37. # sysname - 操作系统名
  38. # nodename - 机器在网络上的名称(需要先设定)
  39. # release - 操作系统发行信息
  40. # version - 操作系统版本信息
  41. # machine - 硬件标识符
  42. # posix.uname_result(sysname='Darwin', nodename='rootMacBook-Air.local', release='20.6.0',
  43. # 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')

爬虫小实践

  1. #!/usr/bin/env python3
  2. #-*- coding: utf-8 -*-
  3. __author__ = '27510299@qq.com'
  4. import urllib.request
  5. def clear():
  6. '''该函数用于清屏'''
  7. print('内容较多,显示3秒后翻页')
  8. time.sleep(3)
  9. OS = platform.system()
  10. if (OS == 'Window'):
  11. os.system('cls')
  12. else:
  13. os.system('clear')
  14. def linkHibobi():
  15. url = 'https://www.baidu.com'
  16. try:
  17. response = urllib.request.urlopen(url, timeout=3)
  18. result = response.read().decode('utf-8')
  19. except Exception as e:
  20. print("网络地址错误")
  21. exit()
  22. with open('hibobi.txt', 'w', encoding='utf8') as fp:
  23. fp.write(result)
  24. print("获取url信息:response.geturl(): %s" %response.geturl())
  25. print("获取返回代码:response.getcode(): %s" %response.getcode())
  26. print("获取返回信息:response.info(): %s" %response.info())
  27. if __name__ == '__main__':
  28. linkHibobi()

2.爬虫小实践

  1. #!/usr/bin/env python3
  2. #-*- coding: utf-8 -*-
  3. __author__ = '27510299@qq.com'
  4. import urllib.request
  5. import sys
  6. import re
  7. def testArgument():
  8. '''测试输入参数,只需要一个参数'''
  9. if len(sys.argv) != 2:
  10. print("只能有一个参数")
  11. tipUp()
  12. exit()
  13. else:
  14. TP = TestProxy(sys.argv[1])
  15. def tipUp():
  16. '''显示提示信息'''
  17. class TestProxy():
  18. '''这个类测试proxy是否有效'''
  19. def clear():
  20. '''该函数用于清屏'''
  21. print('内容较多,显示3秒后翻页')
  22. time.sleep(3)
  23. OS = platform.system()
  24. if (OS == 'Window'):
  25. os.system('cls')
  26. else:
  27. os.system('clear')
  28. def linkHibobi():
  29. url = 'https://www.baidu.com'
  30. try:
  31. response = urllib.request.urlopen(url, timeout=3)
  32. result = response.read().decode('utf-8')
  33. except Exception as e:
  34. print("网络地址错误")
  35. exit()
  36. with open('hibobi.txt', 'w', encoding='utf8') as fp:
  37. fp.write(result)
  38. print("获取url信息:response.geturl(): %s" %response.geturl())
  39. print("获取返回代码:response.getcode(): %s" %response.getcode())
  40. print("获取返回信息:response.info(): %s" %response.info())
  41. if __name__ == '__main__':
  42. linkHibobi()