|
|
#1 |
|
open lots of files without using filenames
I have a large number of directories with non-sequential numbers for names; Each directory contains a variable number of files - all of the same type. I need to open all the files - one at a time. Can I do this without specifying any of the names?
I guess I could make a list of all the directories; then use system calls to ls to get lists of the files in each directory; then read the files. Doesn't seem to terrible but I think microsoft C used to have routines that would do this for you. |
|
|
|
0
|
|
|
#3 |
|
Read the man page for the Standard C Library function opendir().
You may also have to read the header file <dirent.h> to learn the members of struct dirent. http://en.wikipedia.org/wiki/Dirent.h |
|
|
|
0
|
|
|
#4 |
|
Or keep it simple and have your program work on a single file at a time, and pass this via command line with the results of find. If your program needs a whole directory or all the files to do its work, you could get the whole list of files and read in this "index" from stdin or a file. Saves you the directory structure traversal (which isn't *that* bad).
These may not be options here, but just keep in mind that there are many powerful tools you can leverage instead of poorly reinventing them. Astronomy, stocks, or something new? -Lee |
|
|
|
0
|
|
|
#5 | |
|
Quote:
Code:
import os, glob
path = '/tmp/foo'
def process_file(file):
print "processing file: " + file
# Do stuff to file
def scan_dirs(path):
for current in glob.glob( os.path.join(path, '*') ):
if os.path.isdir(current):
print 'processing directory: ' + current
scan_dirs(current)
else:
process_file(current)
if __name__ == "__main__":
scan_dirs(path)
|
||
|
|
0
|
|
|
#6 |
|
If I understand correctly, you just want to iterate through directories and perform some operation on each file?
If so (and you're using Cocoa), you could take a look at NSFileManager; either use the contentsOfDirectoryAtPath:error method, or else the enumeratorAtPath: method (though note that performs a deep iteration, into the sub directories).
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief" |
|
|
|
0
|
|
|
#7 |
|
Err... you didn't say what exactly you're trying to do, or even what language/environment you're trying to do it in. Is this a Bash script, a C program, or what? What do you mean by "open" them? You need to generate a file handle for each one to be read as a part of some script? Just open them in Finder? Display their contents? Are each of these directories full of files contained within a larger directory? If so, you can just use "*/*" to refer to each of the files.
If you explain what exactly it is you're trying to do, I'm sure somebody here can figure out how to help you with it. |
|
|
|
0
|
|
|
#8 |
|
Thanks for the help guys. I didn't have response set on immediate so I did it like I said but will look into opendir. I didn't say that this was C so anything that wasn't was out but thanks all the same.
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 04:00 AM.








Linear Mode
