Python join() 方法与os.path.join()的区别

Python join() 方法与os.path.join()的区别

1. 函数作用:

join() :将序列、字符串 、元组等中的元素以指定的字符连接生成一个新的字符串。
os.path.join() : 将多个路径组合后返回

2. join()方法说明:

join()方法
语法:
str.join(sequence)
参数说明:
str:指定的字符,即分隔符
sequence:需要连接的元素

#字符串序列

seq = ("apple", "banana", "pear")

str = ""
print(str.join(seq))
#applebananapear

str = " "
print(str.join(seq))
#apple banana pear

str = "-"
print(str.join(seq))
#apple-banana-pear

3. os.path.join() 函数说明

os.path.join() 函数
语法:
os.path.join(path1[,path2[,……]])

比如:F:\data\input 文件夹下:

import os
path_root = ‘F:\\data\\input‘
dirs = os.listdir(path_root)

# 输出所有文件和文件夹
for file in dirs:
    path = os.path.join(path_root,file)
    path_test = os.path.join(path,‘test‘)
    #print(path)
    print(path_test)
output:
F:\data\input\hamlet.txt
F:\data\input\hamlet2.txt
F:\data\input\input.txt
F:\data\input\test
F:\data\input\userurl_20150911.tdl

4、os.listdir() 方法

作用:os.listdir() 方法 : 返回指定文件夹包含的文件或文件夹名字的列表。该列表顺序以字母排序。

语法:

listdir()方法的语法如下:

os.listdir(path),

path–需要列出的目录路径

import os
path = ‘F:\\data\\input‘
dirs = os.listdir(path)

# 输出所有文件和文件夹
for file in dirs:
   print(file)
output:

hamlet.txt
hamlet2.txt
input.txt
test
userurl_20150911.tdl
参考博客:https://blog.csdn.net/proplume/article/details/79384324

原文地址:https://www.cnblogs.com/ddgjye/p/11204929.html

时间: 2024-08-29 08:13:37

Python join() 方法与os.path.join()的区别的相关文章

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__)"所在脚本

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.

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

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

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')    若

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

os.path.join里写3个变量

注: 必须为变量,不是变量需先定义为变量 dst="/home/ming" Images = "Images" d = open('/log/aa.txt','r')for e in d.readlines():d.close()os.makedirs(os.path.join(dst,e.strip(),Images)) 原文地址:http://blog.51cto.com/yangzhiming/2140126

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