Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 31, 2026.

Fopen help

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
Watch

Alpha

Established Member
Impact
0
fopen

This is the BIGGEST part I struggle with. I don't know how to do it in ANY programming language. If someone could explain to me how I would write the phrase "This is a test" to a file and then output it to the screen I'd be VERY happy :) You can do it in PHP or Python, doesn't matter.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Actually not all languages use fopen for opening files. Here's a few examples of the various methods different languages use. Each example opens file.txt for appending, writes a string to it, reopens the file for reading and outputs the content to the browser. If you want to overwrite the content of the file rather than append to it you can switch the "a" to "w" for both PHP and Python and change the ">>" to ">" for Perl.

PHP
Code:
<?
$file = fopen ("file.txt", "a");
fwrite ($file, "This is a PHP test<br />n");
fclose ($file);

$file = fopen ("file.txt", "r");
$lines = fread ($file, filesize ("file.txt"));
fclose ($file);
echo $lines;
?>

Python
Code:
#!/usr/bin/python

print "Content-type: text/htmlrnrn"

file = open('file.txt','a')
file.write("This is a Python test<br />n")
file.close()

file = open('file.txt','r')
for lines in file.readlines():
    print lines,
file.close()

Perl
Code:
#!/usr/bin/perl

print "Content-type: text/htmlnn";

open (FILE, ">>file.txt");
print FILE "This is a Perl test<br />n";
close (FILE);

open (FILE, "file.txt");
@lines = <FILE>;
close (FILE);
print @lines;
 
0
•••
Thanks very much! :) That will help me get it straight! BTW is it possible to create txt files with Python? So that if a txt file doesnt exist and you try to write to it it will be created?
 
0
•••
Yes Python will attempt to create the file if it doesn't exist when opened for appending or writing.
 
0
•••
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

New in Parking: updated way to act on many domains or inquiries at once
Your domain list and inquiry list now have a bar above and below the list. Check the items you want, then choose an action in the bar...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back