LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2004, 06:28 AM   #1
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Rep: Reputation: 0
Pyhton Directory List


Hi Guys,

I'm trying to get a directory listing so I can run a 'regex' over them something like
sed -e 's/*.*_//' -e 's/\..*$//'` but I can't get them to list. All I get back is []

any ideas?
tia, mark.

#!/usr/bin/env python
import os, sys, dircache
pathname = os.path.dirname(sys.argv[0])
print 'full path =', os.path.abspath(pathname)
listfiles=dircache.listdir("pathname")
print listfiles
 
Old 06-09-2004, 06:42 AM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Your problem is the listfiles=... line. Take the quotes off "pathname". If you have it in quotes, it treats "pathname" as the actual pathname, as if you had typed in something like "/home/facets/dir_that_doesn't_exist". Since it can't find the dir, it returns an empty list. Without the quotes, it will treat pathname as the variable you want it to be.
 
Old 06-09-2004, 06:46 AM   #3
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Original Poster
Rep: Reputation: 0
#!/usr/bin/env python
import os, sys, dircache, string, re
pathname = os.path.dirname(sys.argv[0])
listfiles=dircache.listdir(pathname)

This returns the same output []
Any ideas?
TIA, Mark.
 
Old 06-09-2004, 07:51 AM   #4
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
I copied it straight off, and it works for me...
Code:
$ cat test.py
#!/usr/bin/env python
import os, sys, dircache, string, re
pathname = os.path.dirname(sys.argv[0])
listfiles=dircache.listdir(pathname)

print "First command-line argument = ", sys.argv[0]
print "Path of first argument = ", pathname
print "Files in that path = ", listfiles


$ ./test.py
First command-line argument =  ./test.py
Path of first argument =  .
Files in that path =  ['test.py', 'test.py~']


$ /home/nemo/test/test.py
First command-line argument =  /home/nemo/test/test.py
Path of first argument =  /home/nemo/test
Files in that path =  ['test.py', 'test.py~']
 
Old 06-09-2004, 09:24 AM   #5
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Original Poster
Rep: Reputation: 0
Yup, your right. Sorry!
I was invoking it with python <scriptname> for some reason.

Cheers, Mark.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 06:03 AM
cannot list directory contents kpachopoulos Linux - General 2 11-29-2005 05:24 PM
apache list directory linuxtesting2 Linux - Networking 5 09-24-2004 03:46 PM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
directory list jpostma Linux - General 1 10-23-2003 06:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration