Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:59:58+00:00 2026-06-03T20:59:58+00:00

I am new to python and coding in general. I am trying to read

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:00:00+00:00Added an answer on June 3, 2026 at 9:00 pm

    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 🙂
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to python (well to coding in general) and am trying to
I'm new to Python, coming from a C# background and I'm trying to get
I really new to Python and coding in general, but I have been making
I am new to coding python. I am hoping to modify this code to
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I'm somewhat new to C++, coming from python. One piece of functionality I really
I'm new to C# coming from a python background. I've had a hard time
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1.
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
Im new two python and am trying to grow a dictionary of dictionaries. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.