What is the command for starting files? Something like this im guessing


If you open a file with the open() function to write in, and it doesn't exist, then it will be created automatically. Is this what you're looking for? For example, to use your code:

theFile = open("C:\test.txt","w")

EDIT: Wow, my bad, the post below me ninja'd it. I dunno what I was thinking.

I think this is what you need:

import os

Hope that helps :)

I think this is what you need:

import os

Hope that helps :)

Okay thanks guys I got that but the real problem was that i wanted to know how to get a random line entry from a text file.

something like

for word in (C:\test.txt):

lol thats probably way off and \n probably doesnt mean new line.

Oh, so I was closer to being right than I thought.

Okay, so the code in my post above opened the file for you. In order to get something random, use the random module. Specifically, if you give random.choice(argList) a list as an argument, it'll give you back one of the element of the list at random. If you have a file object (like the one that the code in my above post gets you) then the readlines() method will return the lines for the text in order. You're code above for spliting the whole of the file at newlines (\n) does work, but you don't have the right syntax for it. Here's how I'd do it:

import random
theFile = open("C:\test.txt","r")
randomLine = random.choice(theFile.readlines())
print randomLine

Thank you!

1 more question. How do I start the default web browser to a specific URL in python?

I know you have to import webbrowser. thats it though lol.

Also, how do you name variables according to other variables' values?

like lets say i do:

count = 0

def Loop():
     count += 1
     if count == 4:
           number [count] = 2

up there I tried to name the variable number along with another variables value 1-4, but how do I do that? what if i name the variable numbercount and it doesnt ouput the name as number1 or number 2 but numbercount the whole time? are there some kind of varaible tags? like can i do this: number%count% = 2

1 more question. How do I start the default web browser to a specific URL in python?

>>> import webbrowser
>>> dir(webbrowser)
['BackgroundBrowser', 'BaseBrowser', 'Elinks', 'Error', 'Galeon', 'GenericBrowser', 'Grail', 'Konqueror', 'Mozilla', 'Netscape', 'Opera', 'UnixBrowser', 'WindowsDefault', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_browsers', '_iscommand', '_isexecutable', '_synthesize', '_tryorder', 'browser', 'get', 'iexplore', 'main', 'open', 'open_new', 'open_new_tab', 'os', 'register', 'register_X_browsers', 'shlex', 'stat', 'subprocess', 'sys', 'time']
>>> help(webbrowser.open)
Help on function open in module webbrowser:

open(url, new=0, autoraise=1)

>>> webbrowser.open("http://www.google.com/")

You second question in last post is really a mess,the same with that code example.
I really dont know what you what you mean here.

Just a guess.

count = 4
number = [1,2,3,count]
print number[3]  #4

#number [count] = 2  #this is plain wrong you cant assagin variabels this way

For your second question, yes, there is a way to make arbitrary variable names in python, but you shouldn't need it. How familiar are you with lists and dictionaries? I would pull up a few online tutorials and go over them, as they should suit your needs. If for some reason they don't, look into eval(), as that's how you evaluate arbitrary code.

1st problem:

thanks for explaining that, but how do I specify which web browser to use out of that list? you didn't explain that.

2nd problem:

Okay let me clarify. What I'm asking is, is there some kind of character in python that allows me to assign (for example) numbers 1-4 into a seperate variables name? like lets say i do:

for num in range(1,4):
     #i want the 1-4 pick from the num var to be part of another 
     #variables name
     Entry[I]num[/I] #Entrynum isn't supposed to be called "Entrynum"
     #but either Entry1, Entry2, Entry3, or Entry4. 
     #if i simply do Entrynum and set it to a var 
     #it stays as "Entrynum" and i dont want that

That is what I meant. How can I specify python to set the name of one variable according to a different one? Any 'tags' I can use for that? like:


You can use function exec() ...

# create variables num1, num2, num3 and num4 and set to 77
for n in range(1, 4):
    exec( "%s = %d" % ('num' + str(n), 77) )

print(num1)  # 77
print(num3)  # 77

Doing something like this is not recommend!