473,698 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading files

Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vs ftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readline s()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000 000 +0000
Modify: 2005-12-16 12:34:00.000000 000 +0000
Change: 2005-12-16 12:34:00.000000 000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Regards,

Johhny.

Dec 19 '05 #1
10 2133

Johhny wrote:
Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vs ftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readline s()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000 000 +0000
Modify: 2005-12-16 12:34:00.000000 000 +0000
Change: 2005-12-16 12:34:00.000000 000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Regards,

out of curiosity, what other programming background do you have prior
to use python ?

Dec 19 '05 #2
Johhny wrote:
Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vs ftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readline s()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000 000 +0000
Modify: 2005-12-16 12:34:00.000000 000 +0000
Change: 2005-12-16 12:34:00.000000 000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Yes: Python is doing exactly what you've told it to.

vsftpd is a file object, and what you are seeing is the representation
(repr()) of that object.

Try

print vsftpd.read()

instead and you'll see the content of the file.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 19 '05 #3
"Johhny" <ex*****@hotmai l.com> writes:
===SNIP===
import string
Why's that here? You don't need that import...
vsftpd=open('vs ftpd.conf', 'r')
Open the file and associate its resulting *object* to the 'vsftpd' variable.
print vsftpd
Print the object's __str__.
vsftpd.read()
Read the full file into the string at the LHS (left side of the '='). In your
case, it reads the full file and discard its contents.
vsftpd.readline s()
Read all other lines (none) into an array on the LHS.
vsftpd.close()
Close the file.
When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.


What issue? You did nothing with what you read, just with the object itself.
--
Jorge Godoy <go***@ieee.org >
Dec 19 '05 #4

Steve Holden wrote:
Johhny wrote:
Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vs ftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readline s()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000 000 +0000
Modify: 2005-12-16 12:34:00.000000 000 +0000
Change: 2005-12-16 12:34:00.000000 000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Yes: Python is doing exactly what you've told it to.

vsftpd is a file object, and what you are seeing is the representation
(repr()) of that object.

Try

print vsftpd.read()

instead and you'll see the content of the file.

The obvious is not so obvious for this user.

Dec 19 '05 #5
Thanks for your assistance, Is it proper practice in python to flush
any memory when you exit? for example Ive read the file into memory,
when I close the file do I also have to flush any memory allocations ?

Dec 19 '05 #6

Johhny wrote:
Thanks for your assistance, Is it proper practice in python to flush
any memory when you exit? for example Ive read the file into memory,
when I close the file do I also have to flush any memory allocations ?

No. you don't need to and shouldn't unless you have very very specific
need to "del" things and even that is not flush memory.

Dec 19 '05 #7
On Mon, 19 Dec 2005 02:47:22 -0800, Johhny wrote:
Thanks for your assistance, Is it proper practice in python to flush
any memory when you exit? for example Ive read the file into memory,
when I close the file do I also have to flush any memory allocations ?


You've done this:

fileobject = file("my file", "r")
mytext = fileobject.read ()
fileobject.clos e()

(It is good practice to close the file as soon as you can, especially on
platforms like Windows where open files can't be read by any other process.)

The text you read is still hanging around, waiting for you to use it. Once
you are done with it, you might want to reclaim that memory used,
especially if it is a 100MB text file:

mytext = "" # re-bind the name 'mytext' to the empty string

Now that enormous chunk of text will be collected by the Python garbage
collector, reclaiming the memory used.

But even that is not always needed: if the name 'mytext' is local to a
function, then when the function ends, the block of memory used will be
collected by the Python garbage collector, and you don't have to do a
thing.
--
Steven.

Dec 19 '05 #8
Steven D'Aprano wrote:
[snip..]
(It is good practice to close the file as soon as you can, especially on
platforms like Windows where open files can't be read by any other process.)


Unfortuantely not the case - on windoze (XP SP2 at least) you can open
a file in one process (for reading or writing) and still read (or write
to) it from another process.

FIle locking nightmare ahoy... You can obtain locks from the operating
system though.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Dec 19 '05 #9
Steven D'Aprano <st***@REMOVETH IScyber.com.au> writes:
The text you read is still hanging around, waiting for you to use it. Once
you are done with it, you might want to reclaim that memory used,
especially if it is a 100MB text file:

mytext = "" # re-bind the name 'mytext' to the empty string
If you have no further use for the variable, then "del mytext" will
also free up the memory, and make it clear to the reader that you're
done with the memory.
But even that is not always needed: if the name 'mytext' is local to a
function, then when the function ends, the block of memory used will be
collected by the Python garbage collector, and you don't have to do a
thing.


The immediate collection is implementation-dependent. It works that
way in Cpython. Other implementations may leave the memory allocated
unil there's a GC run. Since I'm dealinng with trivia, whether or not
the OS gets the memory back or it stays part of the python process
varies from platform to platform and implementation to implementation.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2816
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed to the printer to standard in. You have to provide yourself the program that does something with that data, so I thought with some quick c/c++ programming that would be no problem. I tried dozens of ways of reading the data from stdin or cin,...
19
10323
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
1
6756
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
6
6349
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
2
3248
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish between plain ASCII files I receive and the Unicode ones would be to check if the first two bytes read 0xFFFE. But nothing I do seems to be able to do that. I tried reading it in binary mode and reading two characters in:
7
5557
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
6
5267
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
8351
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the delivery of Travis Oliphant's NumPy manual, I have a quick question (hopefully) regarding how to read in Fortran written data. The data files are not binary, but ASCII text files with no formatting and mixed data types (strings, integers,...
5
14985
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
4
1894
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who developed it was making a duplicate of each file and then deleting the prefix on the copied file so the following statement could read a generic "filename":
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.