18

I am new to python and coding in general. I am trying to read from a text file which has path names on each line. I would like to read the text file line by line and split the line strings into drive, path and file name.

Here is my code thus far:

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

I get the following error:

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

I do not receive this error when I only want the path and file name.

Helgi
  • 5,428
  • 1
  • 31
  • 48
Visceral
  • 253
  • 3
  • 4
  • 8

2 Answers2

32

You need to use os.path.splitdrive first:

with open('C:/Users/visc/scratch/scratch_child/test.txt') as f:
    for line in f:
        drive, path = os.path.splitdrive(line)
        path, filename = os.path.split(path)
        print('Drive is %s Path is %s and file is %s' % (drive, path, filename))

Notes:

  • the with statement makes sure the file is closed at the end of the block (files also get closed when the garbage collector eats them, but using with is generally good practice
  • you don't need the brackets - os.path.splitdrive(path) returns a tuple, and this will get automatically unpacked
  • file is the name of a class in the standard namespace and you should probably not overwrite it :)
Helgi
  • 5,428
  • 1
  • 31
  • 48
Manuel Ebert
  • 8,429
  • 4
  • 40
  • 61
  • Hi Nk, I received the following output: Drive is Path is "S:\Entourage\GIS\HemloBelt\Claims and file is Entourage_Claims_Master.shp", so not quite what I expected. Each line is formatted like so: "S:\Entourage\GIS\HemloBelt\Claims\Entourage_Claims_Master.shp", – Visceral May 08 '12 at 22:56
  • I suppose you're on a windows machine. Before `drive, path = ...`, add `line = line.replace("\\", "/")` to replace backslashes with forward slashes and see whether that does the trick. – Manuel Ebert May 09 '12 at 09:43
  • I found out why it was behaving as it did. I had quote's surrounding each line string in my original text file. – Visceral May 09 '12 at 16:52
6

You can use os.path.splitdrive() to get the drive and then path.split() the remainder.

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

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

    print line.strip()
    print('Drive is %s Path is %s and file is %s' % (drive, path, file))
jordanm
  • 33,009
  • 7
  • 61
  • 76
  • Thanks for taking the time to answer. – Visceral May 08 '12 at 22:40
  • Hi Jordanm, this is what was printed on my screen: `code` "S:\Entourage\GIS\HemloBelt\Claims\Entourage_Claims_Master.shp", Drive is Path is "S:\Entourage\GIS\HemloBelt\Claims and file is Entourage_Claims_Master.shp", Not quite what I had in mind. – Visceral May 08 '12 at 23:00