title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
82,841
<pre><code>import os os.path.exists(filename) </code></pre>
85
2008-09-17T12:56:22Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
82,846
<p>You have the <a href="https://docs.python.org/2/library/os.path.html#os.path.exists"><code>os.path.exists</code></a> function:</p> <pre><code>import os.path os.path.exists(file_path) </code></pre> <p>This returns <code>True</code> for both files and directories but you can additionally use <code>os.path.isfile</co...
964
2008-09-17T12:57:08Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
82,852
<p>You can also use <a href="https://docs.python.org/2/library/os.path.html#os.path.isfile"><code>os.path.isfile</code></a></p> <blockquote> <p>Return <code>True</code> if path is an existing regular file. This follows symbolic links, so both <a href="https://docs.python.org/2/library/os.path.html#os.path.islink">is...
2,356
2008-09-17T12:57:51Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
83,012
<p>Additionally, <code>os.access()</code>:</p> <pre><code>if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() </code></pre> <p>Being <code>R_OK</code>, <code>W_OK</code>, and <code>X_OK</code> the flags to test for permissions (<a href="https://docs.python.org/3/library/os.html#os...
18
2008-09-17T13:13:31Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
84,173
<p>Unlike <a href="http://docs.python.org/3.3/library/os.path.html?highlight=os.path#os.path.isfile"><code>isfile()</code></a>, <a href="http://docs.python.org/3.3/library/os.path.html?highlight=os.path#os.path.exists"><code>exists()</code></a> will yield <em>True</em> for directories.<br> So depending on if you want o...
640
2008-09-17T15:01:14Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
1,671,095
<p>Prefer the try statement. It's considered better style and avoids race conditions.</p> <p>Don't take my word for it. There's plenty of support for this theory. Here's a couple:</p> <ul> <li>Style: Section "Handling unusual conditions" of <a href="http://allendowney.com/sd/notes/notes11.txt">http://allendowney.com/...
87
2009-11-04T00:48:06Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
4,247,590
<p>Just to add to the confusion, it seems that the try: open() approach suggested above doesn't work in Python, as file access isn't exclusive, not even when writing to files, c.f. <a href="http://stackoverflow.com/questions/186202/what-is-the-best-way-to-open-a-file-for-exclusive-access-in-python">What is the best way...
16
2010-11-22T16:19:19Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
4,799,818
<p>You could try this: (safer)</p> <pre><code>try: # http://effbot.org/zone/python-with-statement.htm # with is more safe to open file with open('whatever.txt') as fh: # do something with fh except IOError as e: print("({})".format(e)) </code></pre> <p>the ouput would be:</p> <blockquote> ...
33
2011-01-25T23:00:01Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
7,201,731
<pre><code>import os path = /path/to/dir root,dirs,files = os.walk(path).next() if myfile in files: print "yes it exists" </code></pre> <p>This is helpful when checking for several files. Or you want to do a set intersection/ subtraction with an existing list.</p>
6
2011-08-26T08:08:04Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
8,876,254
<p>Use <a href="https://docs.python.org/3.3/library/os.path.html?highlight=os.path#os.path.isfile"><code>os.path.isfile()</code></a> with <a href="https://docs.python.org/3.3/library/os.html?highlight=os.access#os.access"><code>os.access()</code></a>:</p> <pre><code>import os import os.path PATH='./file.txt' if os.p...
156
2012-01-16T05:57:12Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
11,267,256
<p>This sample function will test for a file's presence in a very Pythonic way using try .. except:</p> <pre><code>def file_exists(filename): try: with open(filename) as f: return True except IOError: return False </code></pre>
-2
2012-06-29T18:40:31Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
15,432,665
<p>You can simply use tempfile module to know whether file exists or not:</p> <pre><code>import tempfile tempfile._exists('filename') # return True or False </code></pre>
-3
2013-03-15T12:36:34Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
15,818,776
<p>You should definitely use this one.</p> <pre><code>from os.path import exists if exists("file") == True: print "File exists." elif exists("file") == False: print "File doesn't exist." </code></pre>
4
2013-04-04T18:21:11Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
17,344,732
<p>This is the simplest way to check if a file exists. Just <strong>because</strong> the file existed when you checked doesn't <strong>guarantee</strong> that it will be there when you need to open it.</p> <pre><code>import os fname = "foo.txt" if os.path.isfile(fname): print "file does exist at this time" else: ...
78
2013-06-27T13:38:04Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
18,994,918
<p>It doesn't seem like there's a meaningful functional difference between try/except and <code>isfile()</code>, so you should use which one makes sense.</p> <p>If you want to read a file, if it exists, do</p> <pre><code>try: f = open(filepath) except IOError: print 'Oh dear.' </code></pre> <p>But if you jus...
44
2013-09-25T01:52:46Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
20,005,069
<p>If you want to do what in bash would be</p> <pre><code>if [ -e '$FILE' ]; then prog '$FILE' fi </code></pre> <p>which I sometimes do when using Python to do more complicated manipulation of a list of names (as I sometimes need to use Python for), the try open(file): except: method isn't really what's wanted, a...
2
2013-11-15T15:52:22Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
21,641,213
<p><strong>Python 3.4</strong> has an object-oriented path module: <a href="http://docs.python.org/3.4/library/pathlib.html"><strong>pathlib</strong></a>. Using this new module, you can check whether a file exists like this:</p> <pre><code>import pathlib p = pathlib.Path('path/to/file') if p.is_file(): # or p.is_dir...
65
2014-02-08T02:38:42Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
21,688,350
<p>You can write Brian's suggestion without the <code>try:</code>.</p> <pre><code>from contextlib import suppress with suppress(IOError), open('filename'): process() </code></pre> <p><code>supress</code> is part of Python 3.4. In older releases you can quickly write your own supress:</p> <pre><code>from contex...
9
2014-02-10T21:30:20Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
23,826,292
<p>You can use following open method to check if file exists + readable</p> <pre><code>open(inputFile, 'r') </code></pre>
7
2014-05-23T10:01:20Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
26,335,110
<p>If the file is for opening you could use one of the following techniques:</p> <pre><code>&gt;&gt;&gt; with open('somefile', 'xt') as f: #Using the x-flag, Python3.3 and above ... f.write('Hello\n') &gt;&gt;&gt; if not os.path.exists('somefile'): ... with open('somefile', 'wt') as f: ... f.write("H...
4
2014-10-13T07:45:16Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
26,433,646
<p>To check if a file exists, </p> <pre><code>from sys import argv from os.path import exists script, filename = argv target = open(filename) print "file exists: %r" % exists(filename) </code></pre>
7
2014-10-17T21:25:30Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
27,581,592
<p>You can use the "OS" library of python.</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; os.path.exists("C:\\Users\\####\\Desktop\\test.txt") True &gt;&gt;&gt; os.path.exists("C:\\Users\\####\\Desktop\\test.tx") False </code></pre>
7
2014-12-20T15:21:40Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
27,661,444
<p>Although I always recommend using <code>try</code> and <code>except</code> statements, here's a few possibilities for you (My personal favourite is using <code>os.access</code>:</p> <ol> <li><p>Try opening the file:</p> <p>Opening the file will always verify the existence of the file. You can make a function just ...
25
2014-12-26T20:05:32Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
29,909,391
<pre><code>if os.path.isfile(path_to_file): try: open(path_to_file) pass except IOError as e: print "Unable to open file" </code></pre> <blockquote> <p>Raising exceptions is considered to be an acceptable, and Pythonic, approach for flow control in your program. Consider handli...
15
2015-04-28T02:45:31Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
30,444,116
<pre><code>import os #Your path here e.g. "C:\Program Files\text.txt" if os.path.exists("C:\..."): print "File found!" else: print "File not found!" </code></pre> <p>Importing <code>os</code> makes it easier to navigate and perform standard actions with your operating system. </p> <p>For reference also see...
53
2015-05-25T18:29:22Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
31,824,912
<pre><code>import os.path def isReadableFile(file_path, file_name): full_path = file_path + "/" + file_name try: if not os.path.exists(file_path): print "File path is invalid." return False elif not os.path.isfile(full_path): print "File does not exist." ...
5
2015-08-05T06:28:27Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
31,932,925
<blockquote> <h1>How do I check whether a file exists, using Python, without using a try statement?</h1> </blockquote> <h2>Recommendations:</h2> <p><strong>suppress</strong></p> <p>Python 3.4 gives us the <a href="https://docs.python.org/3/library/contextlib.html#contextlib.suppress"><code>suppress</code></a> cont...
33
2015-08-11T03:54:25Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
32,288,118
<p>Here's a 1 line Python command for the Linux command line environment. I find this VERY HANDY since I'm not such a hot Bash guy.</p> <pre><code>python -c "import os.path; print os.path.isfile('/path_to/file.xxx')" </code></pre> <p>I hope this is helpful.</p>
11
2015-08-29T16:15:29Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
33,554,911
<p>In <code>Python 3.4</code> the language provides a new module to manage files:</p> <pre><code>import pathlib path = pathlib.Path('path/to/file') if path.is_file(): # if you want to check a directory: path.is_dir() # if it is true, return true on your code </code></pre>
15
2015-11-05T21:15:03Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
34,295,323
<p><strong>Demo</strong>: </p> <pre><code>try: with open('my_settings.dat') as fp: pass except IOError as e: print "Unable to open file" #Does not exist OR no read permissions </code></pre> <p>Handling missing files with IOErrors rather than if statements.An IOError exception will be raised if the fil...
1
2015-12-15T17:10:49Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
35,602,588
<p>In 2016 the best way is still using <code>os.path.isfile</code></p> <pre><code>&gt;&gt;&gt; os.path.isfile('/path/to/some/file.txt') </code></pre> <p>Or in <code>python3</code> you can use <code>pathlib</code></p> <pre><code>import pathlib path = pathlib.Path('/path/to/some/file.txt') if path.is_file(): ... <...
24
2016-02-24T12:44:10Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
36,882,475
<pre><code>import os iffile = os.system('ls filename') ##### returns 0 if success if iffile == 0: print 'file exists' else: print 'file does not exist' </code></pre>
1
2016-04-27T06:54:11Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
37,050,053
<p>I'm the author of a package that's been around for about 10 years, and it has a function that addresses this question directly. Basically, if you are on a non-windows system, it uses <code>Popen</code> to access <code>find</code>. However, if you are on windows, it replicates <code>find</code> with an efficient fi...
5
2016-05-05T12:00:42Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
37,702,905
<p>You can use os.listdir to check if a file is in a certain directory.</p> <pre><code>import os if 'file.ext' in os.listdir('dirpath'): #code </code></pre>
2
2016-06-08T12:45:32Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
38,793,323
<p>Adding one more slight variation which isn't exactly reflected in the other answers.</p> <p>This will handle the case of the <code>file_path</code> being <code>None</code> or empty string.</p> <p></p> <pre><code>def file_exists(file_path): if not file_path: return False elif not os.path.isfile(fil...
2
2016-08-05T15:54:37Z
[ "python", "file", "filesystems" ]
How do I check whether a file exists using Python?
82,831
<p>How do I check whether a file exists, without using the <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try"><code>try</code></a> statement?</p>
2,729
2008-09-17T12:55:00Z
39,932,496
<p>Testing for files and folders with <code>os.path.isfile()</code>, <code>os.path.isdir()</code> and <code>os.path.exists()</code></p> <p>Assuming that the "path" is a valid path, this table shows what is returned by each function for files and folders:</p> <p><a href="http://i.stack.imgur.com/tOs9p.png"><img src="h...
6
2016-10-08T12:43:02Z
[ "python", "file", "filesystems" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
83,800
<p>The <a href="http://support.microsoft.com/kb/311272" rel="nofollow">devcon</a> tool can control the NIC, but not the interface directly. It's a command-line version of the Device Manager applet.</p> <pre><code>devcon disable (id or portion of name) devcon enable (id or portion of name) </code></pre>
0
2008-09-17T14:26:31Z
[ "python", "networking", "windows-xp" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
83,890
<p>this is VB.Net</p> <pre><code>Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId IS NOT NULL") Dim searcher As New ManagementObjectSearcher(scope, objectQuery) Dim os As ManagementObject Dim moColl As ManagementObjectCollection = searcher.Get() ...
0
2008-09-17T14:35:20Z
[ "python", "networking", "windows-xp" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
83,954
<p>I can't seem to find any basic API for controlling interfaces on MSDN, apart from the RAS API's, but I don't think they apply to non-dialup connections. As you suggest yourself, netsh might be an option, supposedly it also has a programmatic interface: <a href="http://msdn.microsoft.com/en-us/library/ms708353(VS.85)...
1
2008-09-17T14:40:45Z
[ "python", "networking", "windows-xp" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
84,073
<p>You may need to use WMI. This may serve as a good starting point: <a href="http://msdn.microsoft.com/en-us/library/aa394595.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa394595.aspx</a></p>
0
2008-09-17T14:52:35Z
[ "python", "networking", "windows-xp" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
84,315
<p>So far I've found the following Python solution:</p> <pre><code>&gt;&gt;&gt; import wmi; c=wmi.WMI() &gt;&gt;&gt; o=c.query("select * from Win32_NetworkAdapter where NetConnectionID='wifi'")[0] &gt;&gt;&gt; o.EnableDevice(1) (-2147217407,) </code></pre> <p>which is translated, AFAIU, to the generic WMI error 0x800...
5
2008-09-17T15:14:44Z
[ "python", "networking", "windows-xp" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
86,611
<p>Using the netsh interface Usage set interface [name = ] IfName [ [admin = ] ENABLED|DISABLED [connect = ] CONNECTED|DISCONNECTED [newname = ] NewName ]</p> <p>Try including everything inside the outer brackets: netsh interface set interface name="thename" admin=disabled conn...
9
2008-09-17T19:20:31Z
[ "python", "networking", "windows-xp" ]
How to programmatically enable/disable network interfaces? (Windows XP)
83,756
<p>I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.</p>
11
2008-09-17T14:22:56Z
4,317,191
<p>I found this .VBS script on the internet. It has the cool advantage of actually working on machines where I cannot get NETSH to work for this purpose.</p> <pre><code>Const ssfCONTROLS = 3 sConnectionName = "Local Area Connection" sEnableVerb = "En&amp;able" sDisableVerb = "Disa&amp;ble" set shellApp = create...
2
2010-11-30T18:34:44Z
[ "python", "networking", "windows-xp" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,016
<p>Maybe you're looking for <code>__len__</code>. If that method exists, then len(a) calls it:</p> <pre><code>&gt;&gt;&gt; class Spam: ... def __len__(self): return 3 ... &gt;&gt;&gt; s = Spam() &gt;&gt;&gt; len(s) 3 </code></pre>
2
2008-09-17T14:46:43Z
[ "python" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,019
<p>It just isn't.</p> <p>You can, however, do:</p> <pre><code>&gt;&gt;&gt; [1,2,3].__len__() 3 </code></pre> <p>Adding a <code>__len__()</code> method to a class is what makes the <code>len()</code> magic work.</p>
10
2008-09-17T14:46:56Z
[ "python" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,038
<p>Well, there actually is a length method, it is just hidden:</p> <pre><code>&gt;&gt;&gt; a_list = [1, 2, 3] &gt;&gt;&gt; a_list.__len__() 3 </code></pre> <p>The len() built-in function appears to be simply a wrapper for a call to the hidden <strong>len</strong>() method of the object.</p> <p>Not sure why they made...
2
2008-09-17T14:48:37Z
[ "python" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,154
<p>Guido's explanation is <a href="http://mail.python.org/pipermail/python-3000/2006-November/004643.html">here</a>:</p> <blockquote> <p>First of all, I chose len(x) over x.len() for HCI reasons (def __len__() came much later). There are two intertwined reasons actually, both HCI:</p> <p>(a) For some operations...
39
2008-09-17T14:59:53Z
[ "python" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,155
<p>The short answer: 1) backwards compatibility and 2) there's not enough of a difference for it to really matter. For a more detailed explanation, read on.</p> <p>The idiomatic Python approach to such operations is special methods which aren't intended to be called directly. For example, to make <code>x + y</code> ...
11
2008-09-17T14:59:54Z
[ "python" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,205
<p>This way fits in better with the rest of the language. The convention in python is that you add <code>__foo__</code> special methods to objects to make them have certain capabilities (rather than e.g. deriving from a specific base class). For example, an object is </p> <ul> <li>callable if it has a <code>__call__</...
4
2008-09-17T15:04:16Z
[ "python" ]
Why isn't the 'len' function inherited by dictionaries and lists in Python
83,983
<p>example:</p> <pre><code>a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works </code></pre> <p>Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me</p>
18
2008-09-17T14:43:18Z
84,337
<p>there is some good info below on why certain things are functions and other are methods. It does indeed cause some inconsistencies in the language.</p> <p><a href="http://mail.python.org/pipermail/python-dev/2008-January/076612.html" rel="nofollow">http://mail.python.org/pipermail/python-dev/2008-January/076612.ht...
2
2008-09-17T15:16:26Z
[ "python" ]
Running multiple sites from a single Python web framework
85,119
<p>What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish "<code>if site == 'site1' ...
3
2008-09-17T16:37:23Z
85,134
<p>Django has this built in. See <a href="http://docs.djangoproject.com/en/dev/ref/contrib/sites/#ref-contrib-sites" rel="nofollow">the sites framework</a>.</p> <p>As a general technique, include a 'host' column in your database schema attached to the data you want to be host-specific, then include the <code>Host</co...
8
2008-09-17T16:39:06Z
[ "python", "frameworks" ]
Running multiple sites from a single Python web framework
85,119
<p>What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish "<code>if site == 'site1' ...
3
2008-09-17T16:37:23Z
85,271
<p>I use CherryPy as my web server (which comes bundled with Turbogears), and I simply run multiple instances of the CherryPy web server on different ports bound to localhost. Then I configure Apache with mod_proxy and mod_rewrite to transparently forward requests to the proper port based on the HTTP request.</p>
2
2008-09-17T16:52:59Z
[ "python", "frameworks" ]
Running multiple sites from a single Python web framework
85,119
<p>What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish "<code>if site == 'site1' ...
3
2008-09-17T16:37:23Z
86,003
<p>Using multiple server instances on local ports is a good idea, but you don't need a full featured web server to redirect HTTP requests. </p> <p>I would use <a href="http://www.apsis.ch/pound/" rel="nofollow">pound</a> as a reverse proxy to do the job. It is small, fast, simple and does exactly what we need here.</p...
1
2008-09-17T18:10:57Z
[ "python", "frameworks" ]
Running multiple sites from a single Python web framework
85,119
<p>What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish "<code>if site == 'site1' ...
3
2008-09-17T16:37:23Z
86,705
<p>Using Django on apache with mod_python, I host multiple (unrelated) django sites simply with the following apache config:</p> <pre><code>&lt;VirtualHost 1.2.3.4&gt; DocumentRoot /www/site1 ServerName site1.com &lt;Location /&gt; SetHandler python-program SetEn...
6
2008-09-17T19:29:30Z
[ "python", "frameworks" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,480
<p>Depends on what you care about. If you mean WALL TIME (as in, the time on the clock on your wall), time.clock() provides NO accuracy because it may manage CPU time. </p>
13
2008-09-17T17:12:29Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,489
<pre><code>clock() -&gt; floating point number Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records. time() -&gt; floating point number Return the current time in seconds since the Epoch. Fractions of a second may be pre...
12
2008-09-17T17:14:08Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,511
<p>The short answer is: most of the time time.clock() will be better. However, if you're timing some hardware (for example some algorithm you put in the GPU), then time.clock() will get rid of this time and time.time() is the only solution left.</p> <p>Note: whatever the method used, the timing will depend on factors ...
33
2008-09-17T17:16:00Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,529
<p>Short answer: use <strong>time.clock()</strong> for timing in Python.</p> <p>On *nix systems, clock() returns the processor time as a floating point number, expressed in seconds. On Windows, it returns the seconds elapsed since the first call to this function, as a floating point number.</p> <p>time() returns the ...
2
2008-09-17T17:17:57Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,533
<p>As of 3.3, <a href="https://docs.python.org/3/library/time.html#time.clock"><em>time.clock()</em> is deprecated</a>, and it's suggested to use <strong><a href="https://docs.python.org/3/library/time.html#time.process_time">time.process_time()</a></strong> or <strong><a href="https://docs.python.org/3/library/time.ht...
91
2008-09-17T17:18:27Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,536
<p><a href="http://stackoverflow.com/questions/85451#85511">Others</a> have answered re: time.time() vs. time.clock(). </p> <p>However, if you're timing the execution of a block of code for benchmarking/profiling purposes, you should take a look at the <a href="https://docs.python.org/library/timeit.html" rel="nofollo...
20
2008-09-17T17:18:56Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,586
<p>On Unix time.clock() measures the amount of CPU time that has been used by the current process, so it's no good for measuring elapsed time from some point in the past. On Windows it will measure wall-clock seconds elapsed since the first call to the function. On either system time.time() will return seconds passed s...
3
2008-09-17T17:24:37Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
85,642
<p>The difference is very platform-specific.</p> <p>clock() is very different on Windows than on Linux, for example.</p> <p>For the sort of examples you describe, you probably want the "timeit" module instead.</p>
5
2008-09-17T17:32:27Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
87,039
<p>To the best of my understanding, time.clock() has as much precision as your system will allow it.</p>
2
2008-09-17T20:03:49Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
2,246,226
<p>One thing to keep in mind: Changing the system time affects time.time() but not time.clock().</p> <p>I needed to control some automatic tests executions. If one step of the test case took more than a given amount of time, that TC was aborted to go on with the next one.</p> <p>But sometimes a step needed to chang...
16
2010-02-11T17:21:26Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
5,087,351
<p>Use the time.time() is preferred.</p>
0
2011-02-23T05:30:55Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
21,178,451
<p>For my own <code>practice. time()</code> has better precision than <code>clock()</code> on Linux. <code>clock()</code> only has precision less than 10 ms. While <code>time()</code> gives prefect precision. My test is on CentOS 6.4, python 2.6</p> <pre><code>using time(): 1 requests, response time: 14.1749382019 ...
9
2014-01-17T05:21:01Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
21,374,146
<p>Comparing test result between Ubuntu Linux and Windows 7.</p> <p><strong>On Ubuntu</strong></p> <pre><code>&gt;&gt;&gt; start = time.time(); time.sleep(0.5); (time.time() - start) 0.5005500316619873 </code></pre> <p><strong>On Windows 7</strong></p> <pre><code>&gt;&gt;&gt; start = time.time(); time.sleep(0.5); (...
0
2014-01-27T06:13:18Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
35,929,913
<p>I use this code to compare 2 methods .My OS is windows 8 , processor core i5 , RAM 4GB</p> <pre><code>import time def t_time(): start=time.time() time.sleep(0.1) return (time.time()-start) def t_clock(): start=time.clock() time.sleep(0.1) return (time.clock()-start) counter_time=0 cou...
1
2016-03-11T00:15:17Z
[ "python" ]
Python - time.clock() vs. time.time() - accuracy?
85,451
<p>Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?</p> <p>for example:</p> <pre><code>start = time.clock() ... do something elapsed = (time.clock() - start) </code></pre> <p>vs.</p> <pre><code>start = time.time() ... do something elapsed = (time.time() - s...
333
2008-09-17T17:09:13Z
38,482,635
<p>To extend on @Hill's results, here's a test using python 2.7.6 on Xubuntu 14.04 through wine:<br> (timeit.default_timer will use time.clock() because it sees the OS as 'win32')</p> <pre><code>&gt;&gt;&gt; from time import sleep, time, clock &gt;&gt;&gt; start = clock(); sleep(0.5); (clock() - start) # result is nev...
-1
2016-07-20T13:26:53Z
[ "python" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,608
<p>I don't think there is a built in way to get it from Python itself. </p> <p>My question is, how are you getting the IP information from your network?</p> <p>To get it from your local machine you could parse ifconfig (unix) or ipconfig (windows) with little difficulty.</p>
1
2008-09-17T17:27:49Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,613
<p>You need <a href="http://en.wikipedia.org/wiki/Address_Resolution_Protocol">ARP</a>. Python's standard library doesn't include any code for that, so you either need to call an external program (your OS may have an 'arp' utility) or you need to build the packets yourself (possibly with a tool like <a href="http://www...
11
2008-09-17T17:28:21Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,620
<p>Depends on your platform. If you're using *nix, you can use the 'arp' command to look up the mac address for a given IP (assuming IPv4) address. If that doesn't work, you could ping the address and then look, or if you have access to the raw network (using BPF or some other mechanism), you could send your own ARP pa...
0
2008-09-17T17:29:29Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,632
<p>If you want a pure Python solution, you can take a look at <a href="http://www.secdev.org/projects/scapy/" rel="nofollow">Scapy</a> to craft packets (you need to send ARP request, and inspect replies). Or if you don't mind invoking external program, you can use <code>arping</code> (on Un*x systems, I don't know of a...
1
2008-09-17T17:30:54Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,634
<p>It seems that there is not a native way of doing this with Python. Your best bet would be to parse the output of "ipconfig /all" on Windows, or "ifconfig" on Linux. Consider using os.popen() with some regexps.</p>
1
2008-09-17T17:31:18Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,641
<p>You would want to parse the output of 'arp', but the kernel ARP cache will only contain those IP address(es) if those hosts have communicated with the host where the Python script is running.</p> <p>ifconfig can be used to display the MAC addresses of local interfaces, but not those on the LAN.</p>
0
2008-09-17T17:32:21Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
85,707
<p>Mark Pilgrim describes how to do this on Windows for the current machine with the Netbios module <a href="http://groups.google.com/group/comp.lang.python/msg/fd2e7437d72c1c21" rel="nofollow">here</a>. You can get the Netbios module as part of the Win32 package available at python.org. Unfortunately at the moment I...
0
2008-09-17T17:38:25Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
86,541
<p>as python was not meant to deal with OS-specific issues (it's supposed to be interpreted and cross platform), I would execute an external command to do so:</p> <p>in unix the command is <code>ifconfig</code></p> <p>if you execute it as a pipe you get the desired result:</p> <pre><code>import os; myPipe=os.popen2(...
-1
2008-09-17T19:13:45Z
[ "python", "network-programming" ]
Search for host with MAC-address using Python
85,577
<p>I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?</p>
8
2008-09-17T17:23:23Z
89,809
<p><a href="http://www.larsen-b.com/Article/206.html" rel="nofollow">This article</a>, "Send hand-crafted Ethernet Frames in Python (ARP for example)", seems to be exactly what you are looking for.</p>
3
2008-09-18T03:43:03Z
[ "python", "network-programming" ]
How to best implement simple crash / error reporting?
85,985
<p>What would be the best way to implement a simple crash / error reporting mechanism? </p> <p>Details: my app is <strong>cross-platform (mac/windows/linux) and written in Python</strong>, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate...
8
2008-09-17T18:09:32Z
86,007
<p>The web hit is the way to go, but make sure you pick a good URL - your app will be hitting it for years to come. </p>
1
2008-09-17T18:11:53Z
[ "python", "cross-platform", "qa", "error-reporting" ]
How to best implement simple crash / error reporting?
85,985
<p>What would be the best way to implement a simple crash / error reporting mechanism? </p> <p>Details: my app is <strong>cross-platform (mac/windows/linux) and written in Python</strong>, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate...
8
2008-09-17T18:09:32Z
86,010
<p><a href="http://pycrash.sourceforge.net/" rel="nofollow">PyCrash</a>?</p>
1
2008-09-17T18:12:08Z
[ "python", "cross-platform", "qa", "error-reporting" ]
How to best implement simple crash / error reporting?
85,985
<p>What would be the best way to implement a simple crash / error reporting mechanism? </p> <p>Details: my app is <strong>cross-platform (mac/windows/linux) and written in Python</strong>, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate...
8
2008-09-17T18:09:32Z
86,022
<p>Whether you use SMTP or HTTP to send the data, you need to have a username/password in the application to prevent just anyone from sending random data to you.</p> <p>With that in mind, I suspect it would be easier to use SMTP rather than HTTP to send the data.</p>
0
2008-09-17T18:13:09Z
[ "python", "cross-platform", "qa", "error-reporting" ]
How to best implement simple crash / error reporting?
85,985
<p>What would be the best way to implement a simple crash / error reporting mechanism? </p> <p>Details: my app is <strong>cross-platform (mac/windows/linux) and written in Python</strong>, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate...
8
2008-09-17T18:09:32Z
86,050
<p>The web service is the best way, but there are some caveats:</p> <ol> <li>You should always ask the user if it is ok to send error feedback information.</li> <li>You should be prepared to fail gracefully if there are network errors. Don't let a failure to report a crash impede recovery!</li> <li>You should avoid in...
4
2008-09-17T18:16:02Z
[ "python", "cross-platform", "qa", "error-reporting" ]
How to best implement simple crash / error reporting?
85,985
<p>What would be the best way to implement a simple crash / error reporting mechanism? </p> <p>Details: my app is <strong>cross-platform (mac/windows/linux) and written in Python</strong>, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate...
8
2008-09-17T18:09:32Z
86,069
<p>Some kind of simple web service would suffice. You would have to consider security so not just anyone could make requests to your service..</p> <p>On a larger scale we considered a JMS messaging system. Put a serialized object of data containing the traceback/error message into a queue and consume it every x minu...
0
2008-09-17T18:17:52Z
[ "python", "cross-platform", "qa", "error-reporting" ]
How to best implement simple crash / error reporting?
85,985
<p>What would be the best way to implement a simple crash / error reporting mechanism? </p> <p>Details: my app is <strong>cross-platform (mac/windows/linux) and written in Python</strong>, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate...
8
2008-09-17T18:09:32Z
86,167
<blockquote> <p>I can't think of a way to do this without including a username and password for the smtp server in the application...</p> </blockquote> <p>You only need a username and password for authenticating yourself to a smarthost. You don't need it to send mail directly, you need it to send mail through a rel...
3
2008-09-17T18:30:25Z
[ "python", "cross-platform", "qa", "error-reporting" ]
What are the pros and cons of the various Python implementations?
86,134
<p>I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. </p> <p>I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there?</p> <p>I guess what I'm ...
9
2008-09-17T18:25:28Z
86,172
<p>Pros: Access to the libraries available for JVM or CLR.</p> <p>Cons: Both naturally lag behind CPython in terms of features.</p>
1
2008-09-17T18:30:41Z
[ "python" ]
What are the pros and cons of the various Python implementations?
86,134
<p>I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. </p> <p>I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there?</p> <p>I guess what I'm ...
9
2008-09-17T18:25:28Z
86,173
<p><strong>Jython</strong> and <strong>IronPython</strong> are useful if you have an overriding need to interface with existing libraries written in a different platform, like if you have 100,000 lines of Java and you just want to write a 20-line Python script. Not particularly useful for anything else, in my opinion, ...
15
2008-09-17T18:31:21Z
[ "python" ]
What are the pros and cons of the various Python implementations?
86,134
<p>I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. </p> <p>I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there?</p> <p>I guess what I'm ...
9
2008-09-17T18:25:28Z
86,186
<p>All of the implementations are listed here:</p> <p><a href="https://wiki.python.org/moin/PythonImplementations" rel="nofollow">https://wiki.python.org/moin/PythonImplementations</a></p> <p>CPython is the "reference implementation" and developed by Guido and the core developers.</p>
3
2008-09-17T18:33:09Z
[ "python" ]
What are the pros and cons of the various Python implementations?
86,134
<p>I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. </p> <p>I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there?</p> <p>I guess what I'm ...
9
2008-09-17T18:25:28Z
86,427
<p>IronPython and Jython use the runtime environment for .NET or Java and with that comes Just In Time compilation and a garbage collector different from the original CPython. They might be also faster than CPython thanks to the JIT, but I don't know that for sure.</p> <p>A downside in using Jython or IronPython is th...
1
2008-09-17T19:00:15Z
[ "python" ]
What are the pros and cons of the various Python implementations?
86,134
<p>I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. </p> <p>I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there?</p> <p>I guess what I'm ...
9
2008-09-17T18:25:28Z
86,665
<p><a href="http://codespeak.net/pypy/dist/pypy/doc/home.html" rel="nofollow">PyPy</a> is a Python implementation written in RPython wich is a Python subset. </p> <p>RPython can be translated to run on a VM or, unlike standard Python, RPython can be statically compiled.</p>
1
2008-09-17T19:26:12Z
[ "python" ]
What are the pros and cons of the various Python implementations?
86,134
<p>I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. </p> <p>I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there?</p> <p>I guess what I'm ...
9
2008-09-17T18:25:28Z
90,103
<p>An additional benefit for Jython, at least for some, is <a href="http://zyasoft.com/pythoneering/2008/06/realizing-jython-25.html">it lacks the GIL</a> (the Global Interpreter Lock) and uses Java's native threads. This means that you can run pure Python code in parallel, something not possible with the GIL.</p>
6
2008-09-18T04:44:08Z
[ "python" ]
Get list of XML attribute values in Python
87,317
<p>I need to get a list of attribute values from child elements in Python.</p> <p>It's easiest to explain with an example.</p> <p>Given some XML like this:</p> <pre><code>&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="...
10
2008-09-17T20:32:04Z
87,503
<p>I must admit I'm a fan of <a href="http://www.aaronsw.com/2002/xmltramp/" rel="nofollow">xmltramp</a> due to its ease of use.</p> <p>Accessing the above becomes:</p> <pre><code> import xmltramp values = xmltramp.parse('''...''') def getValues( values, category ): cat = [ parent for parent in values['par...
2
2008-09-17T20:47:15Z
[ "python", "xml", "xpath", "parent-child", "xml-attribute" ]
Get list of XML attribute values in Python
87,317
<p>I need to get a list of attribute values from child elements in Python.</p> <p>It's easiest to explain with an example.</p> <p>Given some XML like this:</p> <pre><code>&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="...
10
2008-09-17T20:32:04Z
87,543
<p>You can do this with <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a></p> <pre><code>&gt;&gt;&gt; from BeautifulSoup import BeautifulStoneSoup &gt;&gt;&gt; soup = BeautifulStoneSoup(xml) &gt;&gt;&gt; def getValues(name): . . . return [child['value'] for child in soup.fin...
2
2008-09-17T20:50:51Z
[ "python", "xml", "xpath", "parent-child", "xml-attribute" ]
Get list of XML attribute values in Python
87,317
<p>I need to get a list of attribute values from child elements in Python.</p> <p>It's easiest to explain with an example.</p> <p>Given some XML like this:</p> <pre><code>&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="...
10
2008-09-17T20:32:04Z
87,622
<p>I'm not really an old hand at Python, but here's an XPath solution using libxml2.</p> <pre><code>import libxml2 DOC = """&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="a3"/&gt; &lt;/parent&gt; &lt;parent name...
7
2008-09-17T21:00:32Z
[ "python", "xml", "xpath", "parent-child", "xml-attribute" ]
Get list of XML attribute values in Python
87,317
<p>I need to get a list of attribute values from child elements in Python.</p> <p>It's easiest to explain with an example.</p> <p>Given some XML like this:</p> <pre><code>&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="...
10
2008-09-17T20:32:04Z
87,651
<p>Using a standard W3 DOM such as the stdlib's minidom, or pxdom:</p> <pre><code>def getValues(category): for parent in document.getElementsByTagName('parent'): if parent.getAttribute('name')==category: return [ el.getAttribute('value') for el in parent.getEleme...
3
2008-09-17T21:04:42Z
[ "python", "xml", "xpath", "parent-child", "xml-attribute" ]
Get list of XML attribute values in Python
87,317
<p>I need to get a list of attribute values from child elements in Python.</p> <p>It's easiest to explain with an example.</p> <p>Given some XML like this:</p> <pre><code>&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="...
10
2008-09-17T20:32:04Z
87,658
<p><a href="http://effbot.org/zone/elementtree-13-intro.htm">ElementTree 1.3</a> (unfortunately not 1.2 which is the one included with Python) <a href="http://effbot.org/zone/element-xpath.htm">supports XPath</a> like this:</p> <pre><code>import elementtree.ElementTree as xml def getValues(tree, category): parent...
5
2008-09-17T21:05:38Z
[ "python", "xml", "xpath", "parent-child", "xml-attribute" ]
Get list of XML attribute values in Python
87,317
<p>I need to get a list of attribute values from child elements in Python.</p> <p>It's easiest to explain with an example.</p> <p>Given some XML like this:</p> <pre><code>&lt;elements&gt; &lt;parent name="CategoryA"&gt; &lt;child value="a1"/&gt; &lt;child value="a2"/&gt; &lt;child value="...
10
2008-09-17T20:32:04Z
87,726
<p>My preferred python xml library is <a href="http://codespeak.net/lxml" rel="nofollow">lxml</a> , which wraps libxml2.<br /> Xpath does seem the way to go here, so I'd write this as something like:</p> <pre><code>from lxml import etree def getValues(xml, category): return [x.attrib['value'] for x in ...
2
2008-09-17T21:12:16Z
[ "python", "xml", "xpath", "parent-child", "xml-attribute" ]
If it is decided that our system needs an overhaul, what is the best way to go about it?
87,522
<p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that...
3
2008-09-17T20:48:45Z
87,562
<p>Use this as an opportunity to remove unused features! Definitely go with the new language. Call it 2.0. It will be a lot less work to rebuild the 80% of it that you really need.</p> <p>Start by wiping your brain clean of the whole application. Sit down with a list of its overall goals, then decide which feature...
3
2008-09-17T20:53:43Z
[ "python", "asp-classic", "vbscript" ]
If it is decided that our system needs an overhaul, what is the best way to go about it?
87,522
<p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that...
3
2008-09-17T20:48:45Z
87,573
<p>I would not recommend JScript as that is definitely the road less traveled. ASP.NET MVC is rapidly maturing, and I think that you could begin a migration to it, simultaneously ramping up on the ASP.NET MVC framework as its finalization comes through. Another option would be to use something like ASP.NET w/Subsonic o...
0
2008-09-17T20:55:23Z
[ "python", "asp-classic", "vbscript" ]
If it is decided that our system needs an overhaul, what is the best way to go about it?
87,522
<p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that...
3
2008-09-17T20:48:45Z
87,684
<p>Whatever you do, see if you can manage to follow a plan where you do not have to port the application all in one big bang. It is tempting to throw it all away and start from scratch, but if you can manage to do it gradually the mistakes you do will not cost so much and cause so much panic.</p>
2
2008-09-17T21:08:42Z
[ "python", "asp-classic", "vbscript" ]
If it is decided that our system needs an overhaul, what is the best way to go about it?
87,522
<p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that...
3
2008-09-17T20:48:45Z
87,998
<p>It works out better than you'd believe. </p> <p>Recently I did a large reverse-engineering job on a hideous old collection of C code. Function by function I reallocated the features that were still relevant into classes, wrote unit tests for the classes, and built up what looked like a replacement application. It...
3
2008-09-17T21:44:51Z
[ "python", "asp-classic", "vbscript" ]