阅读量:4
在Python中,你可以使用os和shlex库来模拟cd命令并查看历史记录
import os
import shlex
def cd_history():
if not hasattr(cd_history, "history"):
cd_history.history = []
if os.name == "nt": # Windows系统
print("Windows系统的cd命令不支持历史记录。")
else: # Unix/Linux系统
if len(shlex.split(os.environ["PS1"])) > 1:
current_dir = shlex.split(os.environ["PS1"])[-1]
else:
current_dir = os.getcwd()
print(f"当前目录: {current_dir}")
print("cd历史记录:")
for index, dir_change in enumerate(cd_history.history, start=1):
print(f"{index}. {dir_change}")
if __name__ == "__main__":
cd_history()
这个脚本将显示当前目录以及cd命令的历史记录。请注意,这个脚本仅适用于Unix/Linux系统。在Windows系统中,cd命令不支持历史记录。
以上就是关于“python中cd命令怎么查看历史记录”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm