Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: Making a txt File And Adding Text To It
#1
Code:
x = open("file.txt", "w")
y = raw_input()
x.write(y)
x.close
x = open("file.txt", "r")
x.read
Reply
#2
interesting........thank you good share!
CHINAMAN
Quote:i like to make myself belie
[Image: 2n7ehx4.jpg]
[Image: save_banner.asp?file=HWHERMJMMH.jpg]
Reply
#3
Thanks for this, could you use this to read settings?
Reply
#4
(10-29-2009, 02:19 AM)fail ninja Wrote: Thanks for this, could you use this to read settings?

Code:
import time, re, sys

class ResBot_Main( object ):
    
    def __init__( self, ConfigurationFile ):
        self.Configuration_File_Values = {}
        if os.path.exists( ConfigurationFile ):
            self.ResBot_Config_Reader( ConfigurationFile )
        else:
            self.ResBot_Error( True, "Configuration File...\"%s\" Does not exist" % ( ConfigurationFile ) )

    def ResBot_Error( self, Fatal, Error_Message ):
        if Fatal == True:
            print ( "[ResBot]Fatal Error: %s\n\tRecieved On: %s\nShutting Down..." % ( Error_Message, time.strftime( "%H:%M:%S" ) ) )
            sys.exit( 1 )
        else:
            print ( "[ResBot]Error: %s\n\tRecieved On: %s" % ( Error_Message, time.strftime( "%H:%M:%S" ) ) )
            
    def ResBot_Config_Reader(self, ConfigurationFile ):
        try:
            Config_File_Handle = open( ConfigurationFile, "r" )
        except Exception:
            self.ResBot_Error( True, "Unable to read Configuration File...\"%s\"" % ( ConfigurationFile ) )
        Config_File_Buffer = Config_File_Handle.read( )
        Config_File_Line_Counter = 0
        for Config_Line in Config_File_Buffer.split( "\n" ):
            Config_File_Line_Counter += 1
            Config_Line = re.sub( "[\s'\"]", "", Config_Line )
            if Config_Line[ 0 ] == "#":
                continue
            Config_Line_Key_Value = Config_Line.split( "=" )
            try:
                self.Configuration_File_Values[ Config_Line_Key_Value[ 0 ] ] = Config_Line_Key_Value[ 1 ]
            except Exception:
                self.ResBot_Error( False, "Syntax Error in \"%s\" on Line %i..." % ( ConfigurationFile, Config_File_Line_Counter ) )
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#5
(10-31-2009, 08:27 AM)Fallen Wrote:
Code:
import time, re, sys

class ResBot_Main( object ):
    
    def __init__( self, ConfigurationFile ):
        self.Configuration_File_Values = {}
        if os.path.exists( ConfigurationFile ):
            self.ResBot_Config_Reader( ConfigurationFile )
        else:
            self.ResBot_Error( True, "Configuration File...\"%s\" Does not exist" % ( ConfigurationFile ) )

    def ResBot_Error( self, Fatal, Error_Message ):
        if Fatal == True:
            print ( "[ResBot]Fatal Error: %s\n\tRecieved On: %s\nShutting Down..." % ( Error_Message, time.strftime( "%H:%M:%S" ) ) )
            sys.exit( 1 )
        else:
            print ( "[ResBot]Error: %s\n\tRecieved On: %s" % ( Error_Message, time.strftime( "%H:%M:%S" ) ) )
            
    def ResBot_Config_Reader(self, ConfigurationFile ):
        try:
            Config_File_Handle = open( ConfigurationFile, "r" )
        except Exception:
            self.ResBot_Error( True, "Unable to read Configuration File...\"%s\"" % ( ConfigurationFile ) )
        Config_File_Buffer = Config_File_Handle.read( )
        Config_File_Line_Counter = 0
        for Config_Line in Config_File_Buffer.split( "\n" ):
            Config_File_Line_Counter += 1
            Config_Line = re.sub( "[\s'\"]", "", Config_Line )
            if Config_Line[ 0 ] == "#":
                continue
            Config_Line_Key_Value = Config_Line.split( "=" )
            try:
                self.Configuration_File_Values[ Config_Line_Key_Value[ 0 ] ] = Config_Line_Key_Value[ 1 ]
            except Exception:
                self.ResBot_Error( False, "Syntax Error in \"%s\" on Line %i..." % ( ConfigurationFile, Config_File_Line_Counter ) )

lol Fallen he wont even know what you're trying to show him in that script ^^
[Image: sig.php]
Reply
#6
(11-04-2009, 11:50 PM)Nyx- Wrote: lol Fallen he wont even know what you're trying to show him in that script ^^

How could you possibly know that?
Reply
#7
(10-31-2009, 08:27 AM)Fallen Wrote:
Code:
import time, re, sys

class ResBot_Main( object ):
    
    def __init__( self, ConfigurationFile ):
        self.Configuration_File_Values = {}
        if os.path.exists( ConfigurationFile ):
            self.ResBot_Config_Reader( ConfigurationFile )
        else:
            self.ResBot_Error( True, "Configuration File...\"%s\" Does not exist" % ( ConfigurationFile ) )

    def ResBot_Error( self, Fatal, Error_Message ):
        if Fatal == True:
            print ( "[ResBot]Fatal Error: %s\n\tRecieved On: %s\nShutting Down..." % ( Error_Message, time.strftime( "%H:%M:%S" ) ) )
            sys.exit( 1 )
        else:
            print ( "[ResBot]Error: %s\n\tRecieved On: %s" % ( Error_Message, time.strftime( "%H:%M:%S" ) ) )
            
    def ResBot_Config_Reader(self, ConfigurationFile ):
        try:
            Config_File_Handle = open( ConfigurationFile, "r" )
        except Exception:
            self.ResBot_Error( True, "Unable to read Configuration File...\"%s\"" % ( ConfigurationFile ) )
        Config_File_Buffer = Config_File_Handle.read( )
        Config_File_Line_Counter = 0
        for Config_Line in Config_File_Buffer.split( "\n" ):
            Config_File_Line_Counter += 1
            Config_Line = re.sub( "[\s'\"]", "", Config_Line )
            if Config_Line[ 0 ] == "#":
                continue
            Config_Line_Key_Value = Config_Line.split( "=" )
            try:
                self.Configuration_File_Values[ Config_Line_Key_Value[ 0 ] ] = Config_Line_Key_Value[ 1 ]
            except Exception:
                self.ResBot_Error( False, "Syntax Error in \"%s\" on Line %i..." % ( ConfigurationFile, Config_File_Line_Counter ) )

ehh... I don't know what that is.. but this reads a text file
Code:
f = open("file.txt", 'r')
for line in f:
    print line,
Reply
#8
(11-14-2009, 11:31 PM)nevets04 Wrote: ehh... I don't know what that is.. but this reads a text file
Code:
f = open("file.txt", 'r')
for line in f:
    print line,

Its a class in which reads variables from text files, example

[Config File]
Host = "127.0.0.1"
Port = 6667
lol=abc


so then

self.Configuration_File_Values["Host"] = "127.0.0.1"
self.Configuration_File_Values["Port"] = "6667"
self.Configuration_File_Values["lol"] = "abc"

etc
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 1,793 09-30-2019, 12:59 AM
Last Post: samsmith001
  Python to parse text file and get the count of items magicjack89 0 652 06-28-2012, 10:01 AM
Last Post: magicjack89
  Simple Python Python Compiler Canoris 21 8,486 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,271 12-11-2010, 04:13 PM
Last Post: Bursihido
  Any ebooks for Web Python like making BOF etc? C!RCU!T tr0jAn ☠ 4 1,277 05-12-2010, 02:11 PM
Last Post: Julie

Forum Jump:


Users browsing this thread: 1 Guest(s)