python - os.getcwd() 返回上一级目录
问题描述
我有一个使用当前工作目录的程序。
我正在运行的文件的路径是:/home/pi/Test/file.py
运行 os.getcwd() 时返回的路径是 /home/pi/
我想要的路径是 /home/pi/Test
我错过了什么?
osDir = os.getcwd()
month = osDir + "/" + month
print (osDir)
print (month)
if not os.path.exists(month):
os.makedirs(month)
使用 Python3 。
运行文件的命令是 pi@raspberrypi:~ $ python3 TEST/file.py
解决方案
os.getcwd()
不返回脚本的路径;在您的情况下,它返回脚本启动的文件夹的路径/home/pi
。如果要获取 python 脚本的绝对路径,请os.path.abspath(os.path.realpath(__file__))
改用。
而不是osDir + "/" + month
你应该使用os.path.join(osDir, month)
.
推荐阅读
- android - 可以在 android 的字符串资源中包含字符串资源 id
- racket - 为什么“和”不能在 Racket-lang 的累积中进行操作?
- tomcat - 使用相同的tomcat单个实例端口运行两个具有不同url的不同项目
- android - Google 登录问题:“消息失败=12500”
- c# - 肥皂响应期望另一个元素(验证失败)
- mysql - MySQL - 在最高分列表中排名用户
- mysql - 如何将bs4产生的列表插入数据库?
- jquery - 我无法截取 iframe 的屏幕截图
- java - 具有多个点分组分隔符的格式编号
- excel - 使用 AdvancedFilter 查找 Sheets VBA 中的唯一值