小编Vis*_*ral的帖子

将路径字符串拆分为驱动器,路径和文件名部分

我是python和编码的新手.我试图从一个文本文件中读取每行都有路径名.我想逐行读取文本文件,并将行字符串拆分为驱动器,路径和文件名.

这是我到目前为止的代码:

import os,sys, arcpy

## Open the file with read only permit
f = open('C:/Users/visc/scratch/scratch_child/test.txt')

for line in f:
    (drive,path,file) = os.path.split(line)

    print line.strip()
    #arcpy.AddMessage (line.strip())
    print('Drive is %s Path is %s and file is %s' % (drive, path, file))
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

File "C:/Users/visc/scratch/simple.py", line 14, in <module>
    (drive,path,file) = os.path.split(line)
ValueError: need more than 2 values to unpack
Run Code Online (Sandbox Code Playgroud)

当我只想要路径和文件名时,我没有收到此错误.

python split path filepath

17
推荐指数
2
解决办法
4万
查看次数

Python os.makedirs重新创建路径

我想浏览现有路径和文件名的文本文件中的每一行,将字符串分成驱动器,路径和文件名.那么我想要做的是将文件及其路径复制到新位置 - 不同的驱动器或附加到现有的文件树(即,如果S:\ A\B\C\D\E\F.shp是原始文件.我希望将它作为C:\ users\visc\A\B\C\D\E\F.shp附加到新位置

由于编程技巧不佳,我继续收到错误:

File "C:\Users\visc\a\b.py", line 28, in <module>
     (destination) = os.makedirs( pathname, 0755 );
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import os,sys,shutil

## Open the file with read only permit
f = open('C:/Users/visc/a/b/c.txt')

destination = ('C:/Users/visc')
# read line by line
for line in f:

     line = line.replace("\\\\", "\\")
     #split the drive and path using os.path.splitdrive
     (drive, pathname) = os.path.splitdrive(line)
     #split the path and fliename using os.path.split
     (pathname, filename) = os.path.split(pathname)
#print the stripped line
     print line.strip()
#print the drive, path, …
Run Code Online (Sandbox Code Playgroud)

python tree append path mkdirs

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

path ×2

python ×2

append ×1

filepath ×1

mkdirs ×1

split ×1

tree ×1