How to print all files in a folder?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • surreptitious
    New Member
    • Jul 2008
    • 2

    How to print all files in a folder?

    Hello. I was wondering how to print all the files in a folder. If possible, could it be like Python prints one file and waits for a confirmation and then moves to the next? By the way, by print, I don't mean like printer paper stuff. I mean by Python writes the stuff out in the Command Prompt thing. My friend told me print in language doesn't mean print as in paper and ink. I use Windows2k and Python 2.5. Thanks in advanced.
    Last edited by surreptitious; Jul 17 '08, 09:15 PM. Reason: Forgot to put OS and Version
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    My friend told me print in language doesn't mean print as in paper and ink.
    Context matters (and you got it wrong) :) Your question is actually a tad vague.

    Are you asking if you want dump the contents of all the file in a folder to standard output? That is, dump the output of one file to standard output, wait for confirmation, dump the second one, and so on?

    I was thinking, "too bad you're not on a *NIX, you could just write a small shell script to repeatedly cat all the files in a directory". You can actually get cat for Windows, as there is a windows port.

    In any case, what you probably want to do is use Python to get a list of all the files in a directory. Look at Python docs and more Googling for examples.

    You can then for each file repeatedly extract it line by line and then dump the line to output. Or call cat on said file.

    Comment

    • kaarthikeyapreyan
      New Member
      • Apr 2007
      • 106

      #3
      Check if this is the one u want to it has some basic things

      Code:
      import os,sys
      
      contents=os.listdir('.') #contents of the current directory
      files =[]
      directory=[]
      for i in contents:
      	if os.path.isfile(i) == True :
      		files.append(i)
      	elif os.path.isdir(i) == True :
      		directory.append(i)
      #printing contents
      for j in files:
      	choice = raw_input("Dou you want to print file  %s (y/n): "%j)
      	if choice == 'y':
      		print "**************************"
      		print "Printing Files %s" %j
      		print "**************************"
      		fileobj = open(j,'r')
      		contents = fileobj.readlines()
      		for k in contents:
      			sys.stderr.write(k)
      	else:
      		pass
      Last edited by kaarthikeyapreyan; Jul 18 '08, 08:56 AM. Reason: Updated to add choice

      Comment

      • surreptitious
        New Member
        • Jul 2008
        • 2

        #4
        Sorry for being vague. Here's an example of what I want to do.

        import pprint

        pprint.pprint(o pen(r'c:\text\s omefile.txt').r eadlines())

        And kaarthikeyaprey an. I think you have it but I don't know how to execute it. I put all of that into a .py file and then what do I do. I also tried typing it out in the Command Line but at the end I just get the ... and nothing happens. Was the directory name the only thing I had to add?

        EDIT: Ok. I made a mistake by changing the ('.') to the folder I wanted to do. Example: ('D:\Modified') . It works but it does my python folder. How do I do it so it does another folder?

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          To run a .py file, you use python <filename.py> <commandline arguments>.

          Comment

          • kaarthikeyapreyan
            New Member
            • Apr 2007
            • 106

            #6
            Originally posted by surreptitious
            EDIT: Ok. I made a mistake by changing the ('.') to the folder I wanted to do. Example: ('D:\Modified') . It works but it does my python folder. How do I do it so it does another folder?
            Hey i think u have to use the escape sequence for the backslash
            ("D:\\Modified" )

            Comment

            Working...