Splitting path strings into drive, path and file name parts

后端 未结 2 1047
小鲜肉
小鲜肉 2020-12-18 20:00

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

2条回答
  •  伪装坚强ぢ
    2020-12-18 20:18

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

提交回复
热议问题