[Tutor] File I/O Help please!!

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon Dec 13 10:01:30 EST 2021


On Sun, 12 Dec 2021 14:16:18 -0800, Marise Miville <marisesea at gmail.com>
declaimed the following:

>[image: Tutor help 1.png]
>
>[image: image.png]

	This is a TEXT-ONLY forum. If possible, cut&paste the TEXT from a
console window, do not do image captures. If you must use an image (which
would mostly be required if having problems with layout of a GUI interface
-- unlikely for a beginner), you will have to acquire some hosting site and
post a link (URL) to the image on that site. Preferably use a site that
does not obscure the contents under some random/hash sequence, but instead
displays the actual file name(s) -- there are those of us who will not open
URLs that are a string of hex-digits; only those with known file types
shown.

>I am very new at this and I am trying to write 2 short programs.
>
>*Program 1: Prepare a list of the primes less than 1500. Save them in
>binary form to a file named 'SmallPrimes.dat'*
>

	Given the two part descriptions these sound a lot like homework. I'm
surprised a beginner's exercise for Python specifies "binary-form" --
that's a somewhat advanced module. Common Python I/O works in
"human-readable" text form.

	This program requires two things: a function/procedure for generating
prime numbers -- with a limit of <1500 even the Sieve of Eratosthenes (sp?)
only has to examine factors up to 750.; and logic to handle the I/O. The
binary requirement means using the	struct	module (you don't mention which
version of Python you are using, but the module hasn't changed that much
over the decades so https://docs.python.org/3.9/library/struct.html should
be usable).

>*Program 2: Open the file 'SmallPrimes.dat' and read them into a list.
>Print them 5 at a time until the list is exhausted.*
>

	Again... the struct module would be needed to read the file if it was
used when writing it. Printing "5 at a time" is probably easiest handled by
printing them one-at-a-time WITHOUT LINE-FEEDS and using counter to
indicate when a line-feed needs to be printed to start the next line. With
a suitable format, you can even get the columns to line up <G>

>This is how far I could get without success. What am I doing wrong? Can
>anyone help me correct my codes?

	Without seeing the code or error messages, there is not much we can
deduce.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list