os.path.join路径拼接的问题

问题一:

import os
a = os.path.join("/test1", "/test2")
print(a)
b = os.path.join("/test1", "test2")
print(b)

 输出:/test2

/test1/test2

使用os.path.join第二个参数的首个字符如果是"/" , 拼接出来的路径会不包含第一个参数

问题二:

os.path.join拼接的路径出现了反斜杠

directory1=‘/opt/apps/upgradePackage‘
directory2=‘icp_v1.8.0‘
directory3=os.path.join(directory1,directory2)

print(‘directory3 : %s‘ %directory3)

# directory3 : /opt/apps/upgradePackage\icp_v1.8.0

拼接的符号成了”\”,研究了半天,发现是第一个目录后边少了”/”,加上”/”,程序执行正确

第一个路径后加了‘/‘    directory1=‘/opt/apps/upgradePackage/‘

打印结果  directory3 : /opt/apps/upgradePackage/icp_v1.8.0

问题三:

  per_house_pic_path = os.path.join(self.path,location)
# path2=self.path  # G:\Crawler and Data\21days_spider\lianjia\images
#
# 处理路径拼接  打印出来的是一个斜杠的  但是系统里是两个斜杠的, 会报错
# per_house_pic_path = per_house_pic_path.replace(‘/‘,‘\\‘)

原文地址:https://www.cnblogs.com/kenD/p/11143547.html

时间: 2024-07-30 01:03:26

os.path.join路径拼接的问题的相关文章

python 中os.path.join 双斜杠的解决办法

这两天在写东西的时候遇到了这个问题,主要是上传图片之后,无法在页面展示,原因就出在用join 拼接的路径中出现了"\"而造成的. >>> import os >>> m = os.path.join('路径','文件名.txt') >>> m '路径\\文件名.txt' >>> m.replace('\\','/') '路径/文件名.txt' >>> m = os.path.join('路径','

python os path join urlretrieve 文件操作

分析下面的操作: import os from urllib3 import request class file_retrieve(): def __init__(self): self.path = os.path.join(os.path.dirname(os.path.dirname(__file__)),'images') print(self.path) if not os.path.exists(self.path): os.mkdir(self.path) print(self.

os.path.join() - 忽略绝对路径前的参数

os.path.join()会忽略第一个绝对路径之前的参数! 示例: >>> import os >>> os.path.join('/home', 'mushroomer') '/home/mushroomer' >>> os.path.join('/home', '/mushroomer') '/mushroomer' >>> os.path.join('/home', './mushroomer') '/home/./mushr

os模块 os.stat('path/filename') os.path.dirname(path) os.path.exists(path)  os.path.join(path1[, path2[, ...]])

提供对操作系统进行调用的接口 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 2 os.chdir("dirname")  改变当前脚本工作目录:相当于shell下cd 3 os.curdir  返回当前目录: ('.') 4 os.pardir  获取当前目录的父目录字符串名:('..') 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录 6 os.removedirs('dirname1')    若

Python——os.path.dirname(__file__) 与 os.path.join(str,str)

Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:\pythonSrc\test\test.py 那么将输出 d:\pythonSrc\test (2).当"print os.path.dirname(__file__)"所在脚本

Python os.path.dirname(__file__) os.path.join(str,str)

Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:\pythonSrc\test\test.py 那么将输出 d:\pythonSrc\test (2).当"print os.path.dirname(__file__)"所在脚本

os.path.join 的用法

Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.join():  将多个路径组合后返回 一.函数说明 1.join()函数 语法:'sep'.join(seq) 参数说明: sep:分隔符.可以为空 seq:要连接的元素序列.字符串.元组.字典等 上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串 返回值:返回一个以分隔符sep连

os.path.join()

os.path.join()函数功能:连接两个或更多的路径名组件 如果各组件名首字母不包含’/’,则函数会自动加上 如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃 如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾 Demo1 import os Path1 = 'home' Path2 = 'develop' Path3 = 'code' Path10 = Path1 + Path2 + Path3 Path20 = os.path.join(Path1,Path2,Pat

os.path.join用法

os.path.join()函数:连接两个或更多的路径名组件 1.如果各组件名首字母不包含’/’,则函数会自动加上 2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃 3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾 Demo1 import os Path1 = 'home'Path2 = 'develop'Path3 = 'code' Path10 = Path1 + Path2 + Path3Path20 = os.path.join(Path1,Path2,Pa