[Tutor] file(), open()
I want to work with simple ASCII text files, here.. I know you get them loaded into Python with file() or open() The arguments for file() and open() are: (filename, mode, buffering). How do you refer to the filename? Do you put it in quotes? Do you put in the file's full directory path? Or do file() and open() refer to the same directory as the one the script is in? Are the mode and buffering things always necessary arguments? For now, I just want to read/manipulate files, not write to them. What do file() and open() create? Do you need to set them equal to some variable? Is one better than the other? I want to create a list, where each list item is a line in the file. (The, my program would do stuff with the list.) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file(), open()
On 5/26/07, Alan Gilfoy <[EMAIL PROTECTED]> wrote: > How do you refer to the filename? Do you put it in quotes? Yes. open("foo") > Do you put > in the file's full directory path? Or do file() and open() refer to > the same directory as the one the script is in? Yes, and yes. If you dont supply the absolute path, the script will read the file from the cwd (current working dir). This is _usually_ the same as the script, but not necessarily. If the cwd is /foo/bar and the script is launched using /bar/foo/foo/foo/script.py, then open("file") will refer too /foo/bar/file, while open("/bar/foo/foo/foo/file") will get the file from the same dir as the script itself. This is handled by the OS. > Are the mode and buffering things always necessary arguments? Both mode and buffering are optional. I have during my 7 years of Python-coding never used the buffering-flag. > What do file() and open() create? Do you need to set them equal to > some variable? Is one better than the other? open() and file() are the same functions. Try "print open.__doc__" in your Python interpreter, and you will see this: Open a file. The mode can be 'r', 'w' or 'a' for reading (default), writing or appending. The file will be created if it doesn't exist when opened for writing or appending; it will be truncated when opened for writing. Add a 'b' to the mode for binary files. Add a '+' to the mode to allow simultaneous reading and writing. If the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size. Add a 'U' to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a '\n' in Python. Also, a file so opened gains the attribute 'newlines'; the value for this attribute is one of None (no newline read yet), '\r', '\n', '\r\n' or a tuple containing all the newline types seen. > I want to create a list, where each list item is a line in the file. > (The, my program would do stuff with the list.) mylist = open("listfile").readlines() -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file(), open()
"Alan Gilfoy" <[EMAIL PROTECTED]> wrote in >I want to work with simple ASCII text files, here.. You may find it worth reading the relevant section in one of the tutorials. The official python one is good or you could try the Handling files topic in mine. Most tutorials will answer all of the questions you ask and give examples. > I know you get them loaded into Python with file() or open() You get access to the file objects, you don;t load anything until you call one of the read methods (or use the file as an iterator) > The arguments for file() and open() are: (filename, mode, > buffering). > How do you refer to the filename? As a string > Do you put it in quotes? Yes, or store it in a variable and use that. > Do you put in the file's full directory path? Yes, or it will use the current working directory as shown by os.getcwd() and can be changed using os.chdir() > Or do file() and open() refer to the same directory > as the one the script is in? Thats often what the cwd is set to but not always. > Are the mode and buffering things always necessary arguments? the default mode is 'r' - ie read - if you want any other mode then you must supply it. I've never actually used buffering, the default will do in nearly all cases. If you do help(open) or help(file) at the python prompt you will get a lot of info including this sat the top: file(name[, mode[, buffering]]) -> file object That tells you that open/file() take a mandatory name, an optional mode and an optional buffering (the []) and that they return a file object ( the -> symbol) Its worth getting to know this notation because it is compact and accurate and is often diosplayed by IDEs with code introspection capabilities(IDLE, Pythonwin, SPE, PyCrust, Eclipse etc) > For now, I just want to read/manipulate files, not write to them. > > What do file() and open() create? file objects. Those objects expose a bunch of methods. Those methods are alspo described when you do help(file) at the >>> prompt. The >>> prompt is also a great place to try out these things to see for yourself exactly how they work, what kind of values to pass etc. Never underestimate the power of the >>> prompt in building code, especiually when its new features that you are using for the first time or things you only use occasionally... > Do you need to set them equal to > some variable? Is one better than the other? If you just want to slurp up the content of a file you can bypass the file variable with something like: data = file('myfile.dat').read() Or if you only want to do a single pass over the lines of the file use: for line in file('myfile.dat'): processLine(line) Otherwise you are better assigning to a variable. If you do you should really put the open within a try/except and close it in a finally as a metter of best practice: try: myFile = open('myfile.dat') process(myFile) except IOError: processError() finally: myFile.close() Its also good practice to close files as soon as possible to ensure you don;t lock other users out from accessing them. > I want to create a list, where each list item is a > line in the file. If you only want to process the lines once you can use a for loop directly as shown above. Otherwise use the readlines() method. All of this and more is covered in my tutorial. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] monitor other running applications with Python?
Hi, searched a bit for this but haven't found much. Is it possible to use Python to monitor the use of other applications? (anything...Word, Excel, browsers, etc.) At minimum, I wan't to know that the application was running, but even better would be some sense of the use or content, such as whether the app was idle or the user was using it, or, for a web browser, what URLs were visited and for how long, etc. Ideally I'd like a cross-platforms approach but expect that might be hard. I know utilities like this are out there, but I have no sense how to go about this in Python, or how difficult it might be. Any ideas are appreciated. Thanks, Che _ Like the way Microsoft Office Outlook works? Youll love Windows Live Hotmail. http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_outlook_0507 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor