跳至主要内容

python 之 OS模块

Python中的OS模块提供了用于创建和删除目录(文件夹),获取其内容,更改和标识当前目录等功能。

http://python101.pythonlibrary.org/chapter16_os.html
https://www.tutorialsteacher.com/python/os-module

os.mkdir()函数:创建一个新目录
>>> os.mkdir('c:\\123')

os.getcwd()函数:获取当前工作目录
>>> os.getcwd()

os.chdir()函数:更改当前工作目录
>>> os.chdir("c:\\123")
>>> os.chdir("..")

os.rmdir()函数:删除空目录,不能删除当前所在目录
>>> os.rmdir("123")

os.listdir()函数:返回指定目录下的所有文件和目录的列表,未指定任何目录,则将返回当前所在目录
>>> os.listdir("C:\python38")
>>> os.listdir()


Python检查文件或目录是否存在 
os.path.exists()
使用path.exists可以快速检查文件或目录是否存在

os.path.isfile()
我们可以使用isfile命令来检查给定的输入是文件还是目录。

os.path.isdir()
如果我们想确认给定路径指向目录,我们可以使用os.path.dir()函数 


  1. import os
  2. from os import path
  3.  
  4. def main():
  5.     # Print the name of the OS
  6.     print(os.name)
  7. #Check for item existence and type
  8. print("Item exists:" + str(path.exists("1.xlsx")))
  9. print("Item is a file: " + str(path.isfile(r"C:\Users\DZL\1.xlsx")))
  10. print("Item is a directory: " + str(path.isdir(r"C:\Users\DZL")))
  11.  
  12. if __name__ == "__main__":
  13.     main()



Python 3.4及更高版本具有用于处理文件系统路径的pathlib模块。它使用面向对象的方法来检查文件是否存在

  1. import pathlib
  2. file = pathlib.Path("1.txt")
  3. if file.exists ():
  4.     print ("File exist")
  5. else:
  6.     print ("File not exist")


os.rename(src,dst)用于重命名文件或目录。它需要两个参数

  1. import os
  2. import shutil
  3. from os import path
  4.  
  5. def main():
  6.  # make a duplicate of an existing file
  7.     if path.exists("123.txt"):
  8.  # get the path to the file in the current directory
  9.         src = path.realpath("123.txt");
  10.  
  11.  # rename the original file
  12.         os.rename('123.txt','career.123.txt')
  13.  
  14. if __name__ == "__main__":
  15.     main()


评论

此博客中的热门博文

MechanicalSoup

用于自动与网站交互的 Python 库。 MechanicalSoup 自动存储和发送 cookie,跟踪重定向,并且可以跟踪链接和提交表单。 它不执行 JavaScript。 https://github.com/MechanicalSoup/MechanicalSoup https://mechanicalsoup.readthedocs.io/en/stable/index.html https://realpython.com/python-web-scraping-practical-introduction/ pip show Mechanicalsoup 找到模块的安装位置 https://stackoverflow.com/questions/54352162/download-file-with-mechanicalsoup # Install dependencies # pip install requests # pip install BeautifulSoup4 # pip install MechanicalSoup # Import libraries import mechanicalsoup import urllib.request import requests from bs4 import BeautifulSoup import re # Create a browser object that can collect cookies browser = mechanicalsoup.StatefulBrowser() browser.open("https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/l2kq/qna") browser.download_link(link_text=".xls",file="D:/ONS_Data.xls" )

端口映射 公网访问内网

https://portforward.com/ Holer 通过安全隧道将位于NAT和防火墙之后的本地服务器暴露给公共Internet。 Holer是一个将原型中的应用映射到公网访问的端口映射软件,支持转发基于TCP协议的报文 https://github.com/wisdom-projects/holer 方式一:使用(公告)的holer映射或者开通holer服务,通过holer客户端软件经 holer服务器实现公网访问。 公开的holer映射详情如下: 访问密钥 访问域名 公网地址 本地地址 使用场景 HOLER_CLIENT-2F8D8B78B3C2A0AE holer65530.wdom.net holer.org:65530 127.0.0.1:8080 网页 HOLER_CLIENT-3C07CDFD1BF99BF2 holer65531.wdom.net holer.org:65531 127.0.0.1:8088 网页 HOLER_CLIENT-2A623FCB6E2A7D1D holer65532.wdom.net holer.org:65532 127.0.0.1:80 网页 HOLER_CLIENT-AF3E6391525F70E4 不适用 holer.org:65533 127.0.0.1:3389 远程桌面 HOLER_CLIENT-822404317F9D8ADD 不适用 holer.org:65534 127.0.0.1:22 SSH协议 HOLER_CLIENT-27DD1389DF1D4DBC 不适用 holer.org:65535 127.0.0.1:3306 数据库 使用Java版本的holer客户端 ①java 1.7或者更高版本 ②下载holer-client.zip 修改配置文件C:\holer-client\conf\holer.conf HOLER_ACCESS_KEY=HOLER_CLIENT-2A623FCB6E2A7D1D HOLER_SERVER_HOST=holer65532.wdom.net ③建议先双击运行C:\holer-client\bin\shutdown.bat,再双击运行C:\holer-client\bin\startup.bat...

Chrome浏览器

谷歌搜索 双引号——精确搜索 冒号后加文件类型——搜索特定类型的结果 关键词 后 site:**——搜索特定网站的关键词 +、-关键词——实现特定需求筛选 Google中/——快捷键入浏览·搜索框 关键词后..——搜索特定范围(地点)关键词 intitle:关键词——搜索特定标题 用 puppeteer 直接运行 chrome 爬 https://github.com/puppeteer/puppeteer Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议 控制 Chrome 或 Chromium 。Puppeteer默认 无头 运行,但可以配置为运行完整(非无头)Chrome 或 Chromium。 了解如何为 Chrome 开发扩展程序 https://developer.chrome.com/docs/extensions/mv3/ 什么是Chrome插件 https://github.com/sxei/chrome-plugin-demo Google Workspace 状态信息中心 https://www.google.com/appsstatus#hl=zh&v=status 此页面提供属于“Google Workspace”的服务的状态信息 谷歌浏览器离线下载 https://support.google.com/chrome/answer/95346?co=GENIE.Platform%3DDesktop&hl=zh-Hans 企业版 https://cloud.google.com/chrome-enterprise/browser/download 也可以在谷歌浏览器 帮助中心 中搜索chrome https://www.google.com/intl/zh-CN/chrome/?standalone=1 chrome 打开新网页时不要覆盖 鼠标 中键(滚轮)点击超链接 ,或者右击超链接,选择新标签页打开, 还有点链接的同时按下 Ctrl 键也可以 谷歌在线翻译网页 http://translate.google.com/translate?u= http://www.dropitproject.com/index.php 打开chrome浏览器按 F6 ,等同于按 ...