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

  • SEARCH
  • Home
  • 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 8085601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:10:33+00:00 2026-06-05T18:10:33+00:00

I have a new problem with a python script. When I try to run

  • 0

I have a new problem with a python script. When I try to run it passing a path as argument to the program it returns the error message: “No such file or directory“. The program is supposed to iterate through a directory specified by the path name to look for textfiles and print out the first two lines.

Yes indeed, it is homework, but I have looked and read alot about os and sys but still don’t get it. Can some of you veterans please help a newbie? Thanks

    #!/usr/bin/python2.7
    #print2lines.py
    """
    program to find txt-files in directory and
    print out the first two lines
    """
    import sys, os

    if (len(sys.argv)>1):
        path = sys.argv[0]
        if os.path.exist(path):
            abspath = os.path.abspath(path):
                dirlist = os.listdir(abspath)
                for filename in dirlist:
                    if (filename.endswith(".txt")):
                        textfile = open(filename, 'r')
                        print filename + ": \n"
                        print textfile.readline(), "\n"
                        print textfile.readline() + "\n"

                    else:   
                        print "passed argument is not valid pathname"
    else:   
        print "You must pass path to directory as argument"
  • 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-05T18:10:36+00:00Added an answer on June 5, 2026 at 6:10 pm

    os.listdir returns a list of the names of files in the directory, but not their path. E.g. you have a directory ‘test’ and files a,b and c in it:

    os.listdir("test") #-> returns ["a", "b", "c"]
    

    if you want to open the files, you need to use the full path:

    from os import path, listdir
    def getpaths(dirname):
        return [path.join(dirname, fname) for fname in listdir(dirname)]
    getpaths("test") #-> returns ["test/a", "test/b", "test/c"]
    

    the full solution to your problem in five lines:

    import sys, os
    dir = sys.argv[1]
    files = [open(os.path.join(dir, f)) for f in os.listdir(dir)]
    first2lines = ['\n'.join(f.read().split("\n")[:2]) for f in files]
    print '\n'.join(first2lines)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python and have hit a problem with an SQL query I'm
I am new to python and I have a serious problem that I cannot
I'm trying to solve the following problem: Say I have a Python script (let's
I am new to python and i have written a script that converts a
I have a python script that I want always to run in the background.
I have new problem. My code: .method public static void Main() cil managed {
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I am having a new little problem; I have a little pointer called: int
I'm new to Vim (almost new) and have some questions. The problem came from
I have the problem I just installed the new Monodevelop 2.2.2 on Windows but

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.