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
listlengths
1
5
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
182,953
<p>Well after a bit of hacking of Tim Golden's script, I have the following which seems to work quite well:</p> <pre><code>import os import win32file import win32con path_to_watch = "." # look at the current directory file_to_watch = "test.txt" # look for changes to a file called test.txt def ProcessNewData( newDat...
10
2008-10-08T14:05:53Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
473,471
<p>It should not work on windows (maybe with cygwin ?), but for unix user, you should use the "fcntl" system call. Here is an example in Python. It's mostly the same code if you need to write it in C (same function names)</p> <pre><code>import time import fcntl import os import signal FNAME = "/HOME/TOTO/FILETOWATCH"...
20
2009-01-23T16:08:40Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
1,867,970
<p>Here is a simplified version of Kender's code that appears to do the same trick and does not import the entire file:</p> <pre><code># Check file for new data. import time f = open(r'c:\temp\test.txt', 'r') while True: line = f.readline() if not line: time.sleep(1) print 'Nothing New' ...
3
2009-12-08T16:09:42Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
3,031,168
<p>Check out <a href="https://github.com/seb-m/pyinotify">pyinotify</a>.</p> <p>inotify replaces dnotify (from an earlier answer) in newer linuxes and allows file-level rather than directory-level monitoring.</p>
19
2010-06-13T05:12:49Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
4,690,739
<p>Did you try using <a href="http://packages.python.org/watchdog/" rel="nofollow">Watchdog</a>?</p> <blockquote> <p>Python API library and shell utilities to monitor file system events.</p> <h3>Directory monitoring made easy with</h3> <ul> <li>A cross-platform API.</li> <li>A shell tool to run command...
164
2011-01-14T11:52:26Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
5,339,877
<p>If you want a multiplatform solution, then check <a href="http://doc.qt.io/qt-4.8/qfilesystemwatcher.html" rel="nofollow">QFileSystemWatcher</a>. Here an example code (not sanitized):</p> <pre><code>from PyQt4 import QtCore @QtCore.pyqtSlot(str) def directory_changed(path): print('Directory Changed!!!') @QtCo...
41
2011-03-17T13:45:31Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
15,071,134
<p>This is another modification of Tim Goldan's script that runs on linux and adds a simple watcher for file modification by using a dict (file=>time).</p> <p>usage: whateverName.py path_to_dir_to_watch</p> <pre><code>#!/usr/bin/env python import os, sys, time def files_to_timestamp(path): files = [os.path.join...
3
2013-02-25T16:05:07Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
18,947,445
<pre><code>ACTIONS = { 1 : "Created", 2 : "Deleted", 3 : "Updated", 4 : "Renamed from something", 5 : "Renamed to something" } FILE_LIST_DIRECTORY = 0x0001 class myThread (threading.Thread): def __init__(self, threadID, fileName, directory, origin): threading.Thread.__init__(self) self.th...
1
2013-09-22T18:40:48Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
23,181,354
<p>This is an example of checking a file for changes. One that may not be the best way of doing it, but it sure is a short way.</p> <p>Handy tool for restarting application when changes have been made to the source. I made this when playing with pygame so I can see effects take place immediately after file save.</p> ...
0
2014-04-20T11:11:19Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
24,410,417
<p>Simplest solution for me is using watchdog's tool watchmedo</p> <p>From <a href="https://pypi.python.org/pypi/watchdog" rel="nofollow">https://pypi.python.org/pypi/watchdog</a> I now have a process that looks up the sql files in a directory and executes them if necessary. </p> <pre><code>watchmedo shell-command \ ...
4
2014-06-25T13:43:37Z
[ "python", "file", "pywin32", "watch" ]
How do I watch a file for changes using Python?
182,197
<p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p> <p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin...
197
2008-10-08T11:12:55Z
39,606,320
<p>Here's an example geared toward watching input files that write no more than one line per second but usually a lot less. The goal is to append the last line (most recent write) to the specified output file. I've copied this from one of my projects and just deleted all the irrelevant lines. You'll have to fill in ...
0
2016-09-21T01:53:44Z
[ "python", "file", "pywin32", "watch" ]
What do I need to import to gain access to my models?
182,229
<p>I'd like to run a script to populate my database. I'd like to access it through the Django database API.</p> <p>The only problem is that I don't know what I would need to import to gain access to this.</p> <p>How can this be achieved?</p>
7
2008-10-08T11:23:54Z
182,275
<p>In addition to your own models files, you need to import your settings module as well.</p>
0
2008-10-08T11:37:18Z
[ "python", "django" ]
What do I need to import to gain access to my models?
182,229
<p>I'd like to run a script to populate my database. I'd like to access it through the Django database API.</p> <p>The only problem is that I don't know what I would need to import to gain access to this.</p> <p>How can this be achieved?</p>
7
2008-10-08T11:23:54Z
182,345
<p>Import your settings module too</p> <pre><code>import os os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings" from mysite.polls.models import Poll, Choice </code></pre> <p>should do the trick.</p>
12
2008-10-08T11:55:46Z
[ "python", "django" ]
What do I need to import to gain access to my models?
182,229
<p>I'd like to run a script to populate my database. I'd like to access it through the Django database API.</p> <p>The only problem is that I don't know what I would need to import to gain access to this.</p> <p>How can this be achieved?</p>
7
2008-10-08T11:23:54Z
182,790
<p>If you use the <code>shell</code> argument to the <code>manage.py</code> script in your project directory, you don't have to import the settings manually:</p> <pre><code>$ cd mysite/ $ ./manage.py shell Python 2.5.2 (r252:60911, Jun 10 2008, 10:35:34) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "cop...
5
2008-10-08T13:38:07Z
[ "python", "django" ]
What do I need to import to gain access to my models?
182,229
<p>I'd like to run a script to populate my database. I'd like to access it through the Django database API.</p> <p>The only problem is that I don't know what I would need to import to gain access to this.</p> <p>How can this be achieved?</p>
7
2008-10-08T11:23:54Z
184,898
<p>This is what I have at the top of one my data loading scripts.</p> <pre><code>import string import sys try: import settings # Assumed to be in the same directory. #settings.DISABLE_TRANSACTION_MANAGEMENT = True except ImportError: sys.stderr.write("Error: Can't find the file 'settings.py' in the directo...
6
2008-10-08T21:00:46Z
[ "python", "django" ]
What do I need to import to gain access to my models?
182,229
<p>I'd like to run a script to populate my database. I'd like to access it through the Django database API.</p> <p>The only problem is that I don't know what I would need to import to gain access to this.</p> <p>How can this be achieved?</p>
7
2008-10-08T11:23:54Z
7,870,288
<p>The cleanest solution is to add django extensions.</p> <pre> (virt1)tsmets@calvin:~/Documents/prive/rugby-club/proposal/kitu$ yolk -l Django - 1.3.1 - active Pygments - 1.4 - active Python - 2.6.5 - active development (/usr/lib/python2.6/lib-dynload) django-extensio...
1
2011-10-24T00:21:03Z
[ "python", "django" ]
Os.path : can you explain this behavior?
182,253
<p>I love Python because it comes batteries included, and I use built-in functions, a lot, to do the dirty job for me.</p> <p>I have always been using happily the os.path module to deal with file path but recently I ended up with unexpected results on Python 2.5 under Ubuntu linux, while dealing with string that repre...
5
2008-10-08T11:32:16Z
182,282
<p>From a <code>os.path</code> documentation:</p> <p><strong>os.path.splitdrive(path)</strong><br /> Split the pathname path into a pair (drive, tail) where drive is either a drive specification or the empty string. On systems which do not use drive specifications, drive will always be the empty string. In all cases, ...
3
2008-10-08T11:38:57Z
[ "python", "path" ]
Os.path : can you explain this behavior?
182,253
<p>I love Python because it comes batteries included, and I use built-in functions, a lot, to do the dirty job for me.</p> <p>I have always been using happily the os.path module to deal with file path but recently I ended up with unexpected results on Python 2.5 under Ubuntu linux, while dealing with string that repre...
5
2008-10-08T11:32:16Z
182,283
<p>See the documentation <a href="http://pydoc.org/2.5.1/posixpath.html" rel="nofollow">here</a>, specifically: </p> <blockquote> <p>splitdrive(p) Split a pathname into drive and path. <strong>On Posix, drive is always empty.</strong></p> </blockquote> <p>So this won't work on a Linux box.</p>
1
2008-10-08T11:39:03Z
[ "python", "path" ]
Os.path : can you explain this behavior?
182,253
<p>I love Python because it comes batteries included, and I use built-in functions, a lot, to do the dirty job for me.</p> <p>I have always been using happily the os.path module to deal with file path but recently I ended up with unexpected results on Python 2.5 under Ubuntu linux, while dealing with string that repre...
5
2008-10-08T11:32:16Z
182,417
<p>If you want to manipulate Windows paths on linux you should use the ntpath module (this is the module that is imported as os.path on windows - posixpath is imported as os.path on linux)</p> <pre><code>&gt;&gt;&gt; import ntpath &gt;&gt;&gt; filepath = r"c:\ttemp\FILEPA~1.EXE" &gt;&gt;&gt; print ntpath.basename(file...
24
2008-10-08T12:11:55Z
[ "python", "path" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
188,427
<p>You could try writing a <a href="http://www.sqlalchemy.org/docs/05/types.html#types_custom" rel="nofollow">custom type</a>, for instance:</p> <pre><code>import sqlalchemy.types as types class UUID(types.TypeEngine): def get_col_spec(self): return "uuid" def bind_processor(self, dialect): d...
-15
2008-10-09T18:01:58Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
812,363
<p><a href="http://blog.sadphaeton.com/2009/01/19/sqlalchemy-recipeuuid-column.html" rel="nofollow">I wrote this</a> and the domain is gone but here's the guts....</p> <p>Regardless of how my colleagues who really care about proper database design feel about UUID's and GUIDs used for key fields. I often find I need to...
40
2009-05-01T17:29:48Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
5,384,215
<p>See also the recipe for <a href="http://docs.sqlalchemy.org/en/rel_0_9/core/custom_types.html?highlight=guid#backend-agnostic-guid-type" rel="nofollow">Backend-agnostic GUID Type</a> in the SQLAlchemy documentation for column types.</p>
21
2011-03-21T21:51:55Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
10,301,680
<p>Unfortunately <a href="http://docs.sqlalchemy.org/en/rel_0_9/core/custom_types.html?highlight=guid#backend-agnostic-guid-type" rel="nofollow">Backend-agnostic GUID Type</a> from the SQLAlchemy documentation for column types does not seem to work for primary keys in SQLite database engines. Not quite as ecumenical as...
4
2012-04-24T16:04:33Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
19,935,248
<p>In case anyone is interested, I've been using Tom Willis answer, but found useful to add a string to uuid.UUID conversion in the process_bind_param method</p> <pre><code>class UUID(types.TypeDecorator): impl = types.LargeBinary def __init__(self): self.impl.length = 16 types.TypeDecorator._...
2
2013-11-12T16:57:36Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
30,604,002
<p>Here is an approach based on the <a href="http://docs.sqlalchemy.org/en/rel_0_9/core/custom_types.html?highlight=guid#backend-agnostic-guid-type" rel="nofollow">Backend agnostic GUID</a> from the SQLAlchemy docs, but using a BINARY field to store the UUIDs in non-postgresql databases.</p> <pre><code>import uuid fr...
1
2015-06-02T18:39:59Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
How can I use UUIDs in SQLAlchemy?
183,042
<p>Is there a way to define a column (primary key) as a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a> in <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> if using <a href="http://www.postgresql.org/">PostgreSQL</a> (Postgres)?</p>
26
2008-10-08T14:26:20Z
32,332,765
<p>I've used the <code>UUIDType</code> from the <code>SQLAlchemy-Utils</code> package: <a href="http://sqlalchemy-utils.readthedocs.org/en/latest/data_types.html#module-sqlalchemy_utils.types.uuid" rel="nofollow">http://sqlalchemy-utils.readthedocs.org/en/latest/data_types.html#module-sqlalchemy_utils.types.uuid</a></p...
4
2015-09-01T13:20:16Z
[ "python", "postgresql", "orm", "sqlalchemy", "uuid" ]
Is this the best way to get unique version of filename w/ Python?
183,480
<p>Still 'diving in' to Python, and want to make sure I'm not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files together in one directory. To prevent duplicate filenames from being over-written, I wrote this little function - and I'm just wondering if ther...
13
2008-10-08T15:50:21Z
183,492
<p>If you want readable names this looks like a good solution.<br /> There are routines to return unique file names for eg. temp files but they produce long random looking names.</p>
1
2008-10-08T15:52:38Z
[ "python", "filenames" ]
Is this the best way to get unique version of filename w/ Python?
183,480
<p>Still 'diving in' to Python, and want to make sure I'm not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files together in one directory. To prevent duplicate filenames from being over-written, I wrote this little function - and I'm just wondering if ther...
13
2008-10-08T15:50:21Z
183,528
<p>Two small changes...</p> <pre><code>base_name, ext = os.path.splitext(file_name) </code></pre> <p>You get two results with distinct meaning, give them distinct names.</p> <pre><code>file_name = "%s_%d%s" % (base_name, str(counter), ext) </code></pre> <p>It isn't faster or significantly shorter. But, when you wa...
2
2008-10-08T16:00:51Z
[ "python", "filenames" ]
Is this the best way to get unique version of filename w/ Python?
183,480
<p>Still 'diving in' to Python, and want to make sure I'm not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files together in one directory. To prevent duplicate filenames from being over-written, I wrote this little function - and I'm just wondering if ther...
13
2008-10-08T15:50:21Z
183,533
<p>Yes, this is a good strategy for readable but unique filenames.</p> <p><strong>One important change</strong>: You should replace <code>os.path.isfile</code> with <code>os.path.lexists</code>! As it is written right now, if there is a directory named /foo/bar.baz, your program will try to overwrite that with the ne...
6
2008-10-08T16:02:08Z
[ "python", "filenames" ]
Is this the best way to get unique version of filename w/ Python?
183,480
<p>Still 'diving in' to Python, and want to make sure I'm not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files together in one directory. To prevent duplicate filenames from being over-written, I wrote this little function - and I'm just wondering if ther...
13
2008-10-08T15:50:21Z
183,582
<p>One issue is that there is a race condition in your above code, since there is a gap between testing for existance, and creating the file. There may be security implications to this (think about someone maliciously inserting a symlink to a sensitive file which they wouldn't be able to overwrite, but your program ru...
22
2008-10-08T16:13:02Z
[ "python", "filenames" ]
Is this the best way to get unique version of filename w/ Python?
183,480
<p>Still 'diving in' to Python, and want to make sure I'm not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files together in one directory. To prevent duplicate filenames from being over-written, I wrote this little function - and I'm just wondering if ther...
13
2008-10-08T15:50:21Z
185,558
<p>if you don't care about readability, uuid.uuid4() is your friend.</p> <pre><code>import uuid def unique_filename(prefix=None, suffix=None): fn = [] if prefix: fn.extend([prefix, '-']) fn.append(str(uuid.uuid4())) if suffix: fn.extend(['.', suffix.lstrip('.')]) return ''.join(fn) </code></pre>
0
2008-10-09T01:07:49Z
[ "python", "filenames" ]
Is this the best way to get unique version of filename w/ Python?
183,480
<p>Still 'diving in' to Python, and want to make sure I'm not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files together in one directory. To prevent duplicate filenames from being over-written, I wrote this little function - and I'm just wondering if ther...
13
2008-10-08T15:50:21Z
691,029
<p>How about </p> <pre><code>def ensure_unique_filename(orig_file_path): from time import time import os if os.path.lexists(orig_file_path): name, ext = os.path.splitext(orig_file_path) orig_file_path = name + str(time()).replace('.', '') + ext return orig_file_path </code></pre> ...
0
2009-03-27T18:47:16Z
[ "python", "filenames" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
183,863
<p><code>//</code> is floor division, it will always give you the integer floor of the result. The other is 'regular' division.</p>
0
2008-10-08T17:19:15Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
183,865
<p>The double slash, <code>//</code>, is floor division:</p> <pre><code>&gt;&gt;&gt; 7//3 2 </code></pre>
2
2008-10-08T17:19:42Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
183,866
<p><code>//</code> implements "floor division", regardless of your type. So <code>1.0/2.0</code> will give <code>0.5</code>, but both <code>1/2</code>, <code>1//2</code> and <code>1.0//2.0</code> will give <code>0</code>.</p> <p>See <a href="https://docs.python.org/whatsnew/2.2.html#pep-238-changing-the-division-opera...
9
2008-10-08T17:19:59Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
183,870
<p>In Python 3.0, <code>5 / 2</code> will return <code>2.5</code> and <code>5 // 2</code> will return <code>2</code>. The former is floating point division, and the latter is floor division, sometimes also called integer division.</p> <p>In Python 2.2 or later in the 2.x line, there is no difference for integers unle...
210
2008-10-08T17:21:37Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
1,648,557
<p>Please refer <a href="http://python-history.blogspot.com/2009/03/problem-with-integer-division.html" rel="nofollow">The Problem with Integer Division</a> for the reason for introducing the <code>//</code> operator to do integer division.</p>
2
2009-10-30T08:10:20Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
1,704,753
<p>As everyone has already answered, <code>//</code> is floor division.</p> <p>Why this is important is that <code>//</code> is unambiguously floor division, in all Python versions from 2.2, including Python 3.x versions.</p> <p>The behavior of <code>/</code> can change depending on:</p> <ul> <li>Active <code>__futu...
13
2009-11-09T23:55:18Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
11,604,247
<p>It helps to clarify for the Python 2.x line, <code>/</code> is neither floor division nor true division. The current accepted answer is not clear on this. <code>/</code> is floor division when both args are int, but is true division when either or both of the args are float.</p> <p>The above tells a lot more truth,...
25
2012-07-22T21:50:35Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
22,487,879
<pre><code>&gt;&gt;&gt; print 5.0 / 2 2.5 &gt;&gt;&gt; print 5.0 // 2 2.0 </code></pre>
6
2014-03-18T18:23:48Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
30,624,406
<p>The answer of the equation is rounded to the next smaller integer or float with .0 as decimal point. </p> <pre><code>&gt;&gt;&gt;print 5//2 2 &gt;&gt;&gt; print 5.0//2 2.0 &gt;&gt;&gt;print 5//2.0 2.0 &gt;&gt;&gt;print 5.0//2.0 2.0 </code></pre>
1
2015-06-03T15:26:43Z
[ "python", "math", "syntax", "operators" ]
In Python, what is the difference between '/' and '//' when used for division?
183,853
<p>Is there a benefit to using one over the other? They both seem to return the same results.</p> <pre><code>&gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2 </code></pre>
144
2008-10-08T17:16:35Z
38,552,114
<p><strong>/</strong> --> Floating point division</p> <p><strong>//</strong> --> Floor division</p> <p>Lets see some examples in both python 2.7 and in Python 3.5.</p> <p>Python 2.7.10 vs. Python 3.5 </p> <pre><code>print (2/3) ----&gt; 0 print (2/3) ----&gt; 0.6666666666666666 </code></pre> <p>Python 2.7.10 vs...
1
2016-07-24T12:39:19Z
[ "python", "math", "syntax", "operators" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
184,107
<p>I would go with Django, if you are comfortable with a Python solution. It's at version 1.0 now, and is maturing nicely, with a large user base and many contributors. Integrating jQuery is no problem, and I've done it without any issues.</p> <p>The only thing is, as far as I can tell, Ruby is much more popular for w...
4
2008-10-08T18:19:25Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
184,157
<p>Based in your reasons, I would go with Ruby. I see that you want some administration tools (scp, ftp client) and Ruby has it (net/sftp and net/ftp libraries).</p> <p>Also, there are great gems like God for monitoring your system, Vlad the Deployer for deploying, etc. And a lot of alternatives in Merb's field, just ...
2
2008-10-08T18:26:52Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
184,278
<p>Sorry, but your question is wrong. People are probably going to vote me down for this one but I want to say it anyway:</p> <p>I wouldn't expect to get an objective answer! Why? That's simple:</p> <ul> <li>All Ruby advocates will tell to use Ruby.</li> <li>All Python advocates will tell to use Python.</li> <li>All ...
9
2008-10-08T18:51:26Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
184,282
<p><strong>Django!</strong></p> <p>Look up the DjangoCon talks on Google/Youtube - Especially "Reusable Apps" (www.youtube.com/watch?v=A-S0tqpPga4)</p> <p>I've been using Django for some time, after starting with Ruby/Rails. I found the Django Community easier to get into (nicer), the language documented with <em>exc...
16
2008-10-08T18:51:52Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
184,376
<p>it depends.</p> <p>php - symfony is a great framework. downsides: php, wordy and directory heavy. propel gets annoying to use. upsides: php is everywhere and labor is cheap. well done framework, and good support. lots of plugins to make your life easier</p> <p>python - django is also a great framework. downs...
5
2008-10-08T19:08:42Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
186,738
<p>To get a feeling of where the Django ecosystem is at currently, you might want to check out</p> <ul> <li><a href="http://djangopeople.net/" rel="nofollow">djangopeople.net</a> (try <a href="http://djangopeople.net/us/ny/" rel="nofollow">djangopeople.net/us/ny</a> for New York state)</li> <li><a href="http://djangog...
2
2008-10-09T10:55:29Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
186,765
<p>My experience with various new technologies over the last ten years leads me to recommend that you make stability of the platform a serious criterion. It's all well and good developing with the latest and greatest framework, but when you find it's moved forward a point version and suddenly the way you have done ever...
0
2008-10-09T11:05:56Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
188,971
<p>All of them will get the job done.</p> <h2>Use the one that you and your team are most familiar with</h2> <p>This will have a far greater impact on the delivery times and stability of your app than any of the other variables.</p>
7
2008-10-09T20:02:49Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
189,012
<p>I have to preface this with my agreeing with Orion Edwards, choose the one your team is most familiar with.</p> <p>However, I also have to note the curious lack of ASP.NET languages in your list. Not to provoke the great zealot army, but where's the beef? .NET is a stable, rapid development platform and the labor p...
1
2008-10-09T20:13:03Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
189,236
<p>Don't get stuck in the mindset of server-side page layout. Consider technologies like SproutCore, GWT or ExtJS which put the layouting code fully on the client, making the server responsible only for data marshalling and processing (and easily replaced).</p> <p>And you really, really need to know which server platf...
1
2008-10-09T21:13:14Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
201,950
<p>It pays not to be biased about your server setup. Any modern web framework worth it's weight in source code has a SQL abstraction layer of some sort. PostgreSQL gets much better performance, and this is coming from a former MySQL partisan.</p> <p>Apache is a beast, both to configure and on your server's resources. ...
0
2008-10-14T16:48:48Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
208,938
<p>Having built apps in Django, I can attest to its utility. If only all frameworks were as elegant (yes Spring, I'm looking at you).</p> <p>However in terms of betting the farm on Django, one thing you need to factor in is that Python 3 will be released shortly. Python 3 is not backwards compatible and there's a risk...
1
2008-10-16T15:07:01Z
[ "python", "ruby-on-rails", "django", "merb" ]
Framework/Language for new web 2.0 sites (2008 and 2009)
184,049
<p>I know I'll get a thousand "Depends on what you're trying to do" answers, but seriously, there really is no solid information about this online yet. Here are my assumptions - I think they're similar for alot of people right now:</p> <ol> <li>It is now October 2008. I want to start writing an application for Janua...
2
2008-10-08T18:07:42Z
616,632
<p>Update: I ended up using, and loving, Django. I'm totally done with PHP - sorry about that. Future readers trying to create a new web 2.0 site (assuming they have a programming background), should greatly consider this setup:</p> <p>Amazon ec2 for hosting ($80/month - not cheap but worth it if you can afford it) ...
0
2009-03-05T21:09:13Z
[ "python", "ruby-on-rails", "django", "merb" ]
How do I check out a file from perforce in python?
184,187
<p>I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first. </p>
13
2008-10-08T18:33:08Z
184,193
<p>Here's what I came up with:</p> <pre><code>import os def CreateNewChangeList(description): "Create a new changelist and returns the changelist number as a string" p4in, p4out = os.popen2("p4 changelist -i") p4in.write("change: new\n") p4in.write("description: " + description) p4in.close() c...
7
2008-10-08T18:33:51Z
[ "python", "scripting", "perforce" ]
How do I check out a file from perforce in python?
184,187
<p>I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first. </p>
13
2008-10-08T18:33:08Z
184,238
<p>You may want to check out the P4Python module. It's available on the perforce site and it makes things very simple.</p>
2
2008-10-08T18:45:24Z
[ "python", "scripting", "perforce" ]
How do I check out a file from perforce in python?
184,187
<p>I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first. </p>
13
2008-10-08T18:33:08Z
184,344
<p>Perforce has Python wrappers around their C/C++ tools, available in binary form for Windows, and source for other platforms:</p> <p><a href="http://www.perforce.com/perforce/loadsupp.html#api">http://www.perforce.com/perforce/loadsupp.html#api</a></p> <p>You will find their documentation of the scripting API to be...
20
2008-10-08T19:02:38Z
[ "python", "scripting", "perforce" ]
How do I check out a file from perforce in python?
184,187
<p>I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first. </p>
13
2008-10-08T18:33:08Z
256,419
<p><a href="http://www.perforce.com/perforce/loadsupp.html#api" rel="nofollow">Perforce's P4 Python module</a> mentioned in another answer is the way to go, but if installing this module isn't an option you can use the -G flag to help parse p4.exe output:</p> <pre><code>p4 [ options ] command [ arg ... ] options: ...
4
2008-11-02T02:28:16Z
[ "python", "scripting", "perforce" ]
How do I check out a file from perforce in python?
184,187
<p>I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first. </p>
13
2008-10-08T18:33:08Z
307,908
<p>Building from p4python source requires downloading and extracting the p4 api recommended for that version. For example, if building the Windows XP x86 version of P4Python 2008.2 for activepython 2.5:</p> <ul> <li>download and extract both the <a href="ftp://ftp.perforce.com/perforce/r08.2/tools/p4python.tgz">p4pyth...
3
2008-11-21T04:55:35Z
[ "python", "scripting", "perforce" ]
How do I check out a file from perforce in python?
184,187
<p>I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first. </p>
13
2008-10-08T18:33:08Z
4,242,307
<p>Remember guys to install the development package for Python for the p4api or it will complain about missing headers. In Ubuntu 10.10, just do a simple:</p> <pre><code>apt-get install python2.6-dev </code></pre> <p>Or</p> <pre><code>apt-get install python3.1-dev </code></pre>
2
2010-11-22T04:33:38Z
[ "python", "scripting", "perforce" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,387
<p>This probably doesn't fully comply with file-naming standards, but here it goes:</p> <pre><code>/^Run[\w]*?\.py$/ </code></pre>
0
2008-10-08T23:45:57Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,388
<pre><code>/^Run.*\.py$/ </code></pre> <p>Or, in python specifically:</p> <pre><code>import re re.match(r"^Run.*\.py$", stringtocheck) </code></pre> <p>This will match "Runfoobar.py", but not "runfoobar.PY". To make it case insensitive, instead use:</p> <pre><code>re.match(r"^Run.*\.py$", stringtocheck, re.I) </cod...
6
2008-10-08T23:46:24Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,393
<p>mabye:</p> <pre><code>^Run.*\.py$ </code></pre> <p>just a quick try</p>
0
2008-10-08T23:47:56Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,397
<p>For a regular expression, you would use:</p> <pre><code>re.match(r'Run.*\.py$') </code></pre> <p>A quick explanation:</p> <ul> <li>. means match any character.</li> <li>* means match any repetition of the previous character (hence .* means any sequence of chars)</li> <li>\ is an escape to escape the explicit dot<...
23
2008-10-08T23:48:59Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,426
<p>Warning:</p> <ul> <li>jobscry's answer ("^Run.?.py$") is incorrect (will not match "Run123.py", for example).</li> <li>orlandu63's answer ("/^Run[\w]*?.py$/") will not match "RunFoo.Bar.py".</li> </ul> <p>(I don't have enough reputation to comment, sorry.)</p>
13
2008-10-09T00:01:25Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,583
<p>If you write a slightly more complex regular expression, you can get an extra feature: extract the bit between "Run" and ".py":</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; regex = '^Run(?P&lt;name&gt;.*)\.py$' &gt;&gt;&gt; m = re.match(regex, 'RunFoo.py') &gt;&gt;&gt; m.group('name') 'Foo' </code></pre> <p>...
2
2008-10-09T01:20:30Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
185,593
<p>I don't really understand why you're after a regular expression to solve this 'problem'. You're just after a way to find all .py files that start with 'Run'. So this is a simple solution that will work, without resorting to compiling an running a regular expression:</p> <pre><code>import os for filename in os.listd...
11
2008-10-09T01:27:29Z
[ "python", "sql", "regex", "like" ]
Regular expression to match start of filename and filename extension
185,378
<p>What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?</p> <p>The regular expression should match any of the following:</p> <pre><code>RunFoo.py RunBar.py Run42.py </code></pre> <p>It should not match:</p> <pre><code>myRunFoo.py Ru...
9
2008-10-08T23:42:27Z
186,927
<p>You don't need a regular expression, you can use glob, which takes wildcards e.g. Run*.py</p> <p>For example, to get those files in your current directory...</p> <pre><code>import os, glob files = glob.glob( "".join([ os.getcwd(), "\\Run*.py"]) ) </code></pre>
4
2008-10-09T11:57:56Z
[ "python", "sql", "regex", "like" ]
MVC model structure in Python
185,389
<p>I'm having problems structuring classes in the Model part of an MVC pattern in my Python app. No matter how I turn things, I keep running into circular imports. Here's what I have:</p> <p><strong>Model/__init__p.y</strong></p> <ul> <li>should hold all Model class names so I can do a "from Model import User" e.g. f...
2
2008-10-08T23:46:30Z
185,411
<p>Generally, we put it all in one file. This isn't Java or C++.</p> <p>Start with a single file until you get some more experience with Python. Unless your files are gargantuan, it will work fine. </p> <p>For example, Django encourages this style, so copy their formula for success. One module for the model. A mo...
3
2008-10-08T23:55:10Z
[ "python", "model-view-controller", "model", "structure" ]
MVC model structure in Python
185,389
<p>I'm having problems structuring classes in the Model part of an MVC pattern in my Python app. No matter how I turn things, I keep running into circular imports. Here's what I have:</p> <p><strong>Model/__init__p.y</strong></p> <ul> <li>should hold all Model class names so I can do a "from Model import User" e.g. f...
2
2008-10-08T23:46:30Z
185,480
<p>I think you have one issue that should be straightened. Circular references often result from a failure to achieve separation of concerns. In my opinion, the database and model modules shouldn't know much about each other, working against an API instead. In this case the database shouldn't directly reference any ...
1
2008-10-09T00:28:18Z
[ "python", "model-view-controller", "model", "structure" ]
MVC model structure in Python
185,389
<p>I'm having problems structuring classes in the Model part of an MVC pattern in my Python app. No matter how I turn things, I keep running into circular imports. Here's what I have:</p> <p><strong>Model/__init__p.y</strong></p> <ul> <li>should hold all Model class names so I can do a "from Model import User" e.g. f...
2
2008-10-08T23:46:30Z
185,692
<p>There is an inconsistency in your specification. You say Database.py needs to import all Model classes to do ORM but then you say the User class need access to the Database to do queries.</p> <p>Think of these as layers of an API. The Database class provides an API (maybe object-oriented) to some physical persisten...
6
2008-10-09T02:22:03Z
[ "python", "model-view-controller", "model", "structure" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
185,941
<p>Updated to only delete files and to used the os.path.join() method suggested in the comments. If you also want to remove subdirectories, uncomment the elif statement.</p> <pre><code>import os, shutil folder = '/path/to/folder' for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) tr...
153
2008-10-09T04:27:21Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
186,049
<p>You might be better off using <code>os.walk()</code> for this.</p> <p><code>os.listdir()</code> doesn't distinguish files from directories and you will quickly get into trouble trying to unlink these. There is a good example of using <code>os.walk()</code> to recursively remove a directory <a href="http://docs.pyth...
8
2008-10-09T05:52:54Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
186,236
<p>Try the shutil module</p> <pre><code>import shutil shutil.rmtree('/path/to/folder') </code></pre> <blockquote> <p>Description: <code>shutil.rmtree(path, ignore_errors=False, onerror=None)</code></p> <p>Docstring: Recursively delete a directory tree.</p> <p>If <code>ignore_errors</code> is set, erro...
125
2008-10-09T07:18:35Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
1,073,382
<p>Expanding on mhawke's answer this is what I've implemented. It removes all the content of a folder but not the folder itself. Tested on Linux with files, folders and symbolic links, should work on Windows as well.</p> <pre><code>import os import shutil for root, dirs, files in os.walk('/path/to/folder'): for f...
50
2009-07-02T09:25:56Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
5,756,937
<p>You can simply do this :</p> <pre><code>import os import glob files = glob.glob('/YOUR/PATH/*') for f in files: os.remove(f) </code></pre> <p>You can of corse use an other filter in you path, for exemple : /YOU/PATH/*.txt for removing all text files in a directory.</p>
81
2011-04-22T15:23:45Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
6,615,332
<p>Using <code>rmtree</code> and recreating the folder could work, but I have run into errors when deleting and immediately recreating folders on network drives.</p> <p>The proposed solution using walk does not work as it uses <code>rmtree</code> to remove folders and then may attempt to use <code>os.unlink</code> on ...
38
2011-07-07T18:25:47Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
12,526,809
<p>This is the only answer so far, which:</p> <ul> <li>removes all symbolic links <ul> <li>dead links</li> <li>links to directories</li> <li>links to files</li> </ul></li> <li>removes subdirectories</li> <li>does not remove the parent directory</li> </ul> <p>Code:</p> <pre><code>for filename in os.listdir(dirpath):...
4
2012-09-21T08:24:58Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
13,666,792
<pre><code>import os import shutil # Gather directory contents contents = [os.path.join(target_dir, i) for i in os.listdir(target_dir)] # Iterate and remove each item in the appropriate manner [shutil.rmtree(i) if os.path.isdir(i) else os.unlink(i) for i in contents] </code></pre>
4
2012-12-02T05:51:33Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
16,340,614
<p>I konw it's an old thread but I have found something interesting from the official site of python. Just for sharing another idea for removing of all contents in a directory. Because I have some problems of authorization when using shutil.rmtree() and I don't want to remove the directory and recreate it. The address ...
6
2013-05-02T14:24:00Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
17,146,855
<p>I used to solve the problem this way:</p> <pre><code>import shutil import os shutil.rmtree(dirpath) os.mkdir(dirpath) </code></pre>
4
2013-06-17T11:52:24Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
20,173,900
<p>As a oneliner:</p> <pre><code>import os # Python 2.7 map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) ) # Python 3+ list( map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) ) ) </code></pre>
10
2013-11-24T11:22:15Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
23,614,332
<p>Yet Another Solution:</p> <pre><code>import sh sh.rm(sh.glob('/path/to/folder/*')) </code></pre>
5
2014-05-12T16:33:30Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
24,844,618
<p><em>Notes: in case someone down voted my answer, I have something to explain here.</em></p> <ol> <li>Everyone likes short 'n' simple answers. However, sometimes the reality is not so simple.</li> <li>Back to my answer. I know <code>shutil.rmtree()</code> could be used to delete a directory tree. I've used it many t...
6
2014-07-19T20:21:55Z
[ "python", "file", "local", "delete-directory" ]
Delete Folder Contents in Python
185,936
<p>How can I delete the contents of a local folder in Python?</p> <p>The current project is for Windows but I would like to see *nix also.</p>
171
2008-10-09T04:22:33Z
37,926,786
<p>This should do the trick just using the OS module to list and then remove!</p> <pre><code>import os DIR = os.list('Folder') for i in range(len(DIR)): os.remove('Folder'+chr(92)+i) </code></pre> <p>Worked for me, any problems let me know! </p>
-1
2016-06-20T15:52:45Z
[ "python", "file", "local", "delete-directory" ]
Extracting unique items from a list of mappings
186,131
<p>He're an interesting problem that looks for the most Pythonic solution. Suppose I have a list of mappings <code>{'id': id, 'url': url}</code>. Some <code>id</code>s in the list are duplicate, and I want to create a new list, with all the duplicates removed. I came up with the following function:</p> <pre><code>def ...
2
2008-10-09T06:28:26Z
186,295
<p>Your example can be rewritten slightly to construct the first dictionary using a generator expression and to remove necessity of construction of another mappings. Just reuse the old ones:</p> <pre><code>def unique_mapping(mappings): return dict((m['id'], m) for m in mappings).values() </code></pre> <p>Although...
4
2008-10-09T07:47:09Z
[ "python", "unique", "duplicate-data" ]
Extracting unique items from a list of mappings
186,131
<p>He're an interesting problem that looks for the most Pythonic solution. Suppose I have a list of mappings <code>{'id': id, 'url': url}</code>. Some <code>id</code>s in the list are duplicate, and I want to create a new list, with all the duplicates removed. I came up with the following function:</p> <pre><code>def ...
2
2008-10-09T06:28:26Z
186,317
<p>There are a couple of things you could improve.</p> <ul> <li><p>You're performing two loops, one over the original dict, and then again over the result dict. You could build up your results in one step instead.</p></li> <li><p>You could change to use a generator, to avoid constructing the whole list up-front. (Us...
2
2008-10-09T07:54:23Z
[ "python", "unique", "duplicate-data" ]
Extracting unique items from a list of mappings
186,131
<p>He're an interesting problem that looks for the most Pythonic solution. Suppose I have a list of mappings <code>{'id': id, 'url': url}</code>. Some <code>id</code>s in the list are duplicate, and I want to create a new list, with all the duplicates removed. I came up with the following function:</p> <pre><code>def ...
2
2008-10-09T06:28:26Z
187,041
<p>I think this can be made simpler still. Dictionaries don't tolerate duplicate keys. Make your list of mappings into a dictionary of mappings. This will remove duplicates.</p> <pre><code>&gt;&gt;&gt; someListOfDicts= [ {'url': 'http://a', 'id': 'a'}, {'url': 'http://b', 'id': 'b'}, {'url': 'http://c...
1
2008-10-09T12:32:04Z
[ "python", "unique", "duplicate-data" ]
What is the best way to open a file for exclusive access in Python?
186,202
<p>What is the most elegant way to solve this:</p> <ul> <li>open a file for reading, but only if it is not already opened for writing</li> <li>open a file for writing, but only if it is not already opened for reading or writing</li> </ul> <p>The built-in functions work like this</p> <pre><code>&gt;&gt;&gt; path = r"...
29
2008-10-09T06:58:54Z
186,300
<p>To make you safe when opening files within one application, you could try something like this:</p> <pre><code>import time class ExclusiveFile(file): openFiles = {} fileLocks = [] class FileNotExclusiveException(Exception): pass def __init__(self, *args): sMode = 'r' sFileN...
1
2008-10-09T07:48:44Z
[ "python", "file", "locking" ]
What is the best way to open a file for exclusive access in Python?
186,202
<p>What is the most elegant way to solve this:</p> <ul> <li>open a file for reading, but only if it is not already opened for writing</li> <li>open a file for writing, but only if it is not already opened for reading or writing</li> </ul> <p>The built-in functions work like this</p> <pre><code>&gt;&gt;&gt; path = r"...
29
2008-10-09T06:58:54Z
186,464
<p>I don't think there is a fully crossplatform way. On unix, the fcntl module will do this for you. However on windows (which I assume you are by the paths), you'll need to use the win32file module.</p> <p>Fortunately, there is a portable implementation (<a href="https://github.com/WoLpH/portalocker" rel="nofollow"...
16
2008-10-09T09:00:09Z
[ "python", "file", "locking" ]
What is the best way to open a file for exclusive access in Python?
186,202
<p>What is the most elegant way to solve this:</p> <ul> <li>open a file for reading, but only if it is not already opened for writing</li> <li>open a file for writing, but only if it is not already opened for reading or writing</li> </ul> <p>The built-in functions work like this</p> <pre><code>&gt;&gt;&gt; path = r"...
29
2008-10-09T06:58:54Z
188,827
<p>Here's a start on the win32 half of a portable implementation, that does not need a seperate locking mechanism.</p> <p>Requires the <a href="http://python.net/crew/mhammond/win32/" rel="nofollow">Python for Windows Extensions</a> to get down to the win32 api, but that's pretty much mandatory for python on windows a...
2
2008-10-09T19:30:33Z
[ "python", "file", "locking" ]
What is the best way to open a file for exclusive access in Python?
186,202
<p>What is the most elegant way to solve this:</p> <ul> <li>open a file for reading, but only if it is not already opened for writing</li> <li>open a file for writing, but only if it is not already opened for reading or writing</li> </ul> <p>The built-in functions work like this</p> <pre><code>&gt;&gt;&gt; path = r"...
29
2008-10-09T06:58:54Z
195,021
<blockquote> <p>The solution should work inside the same process (like in the example above) as well as when another process has opened the file.</p> </blockquote> <p>If by 'another process' you mean 'whatever process' (i.e. not your program), in Linux there's no way to accomplish this relying only on system calls (...
6
2008-10-12T02:46:56Z
[ "python", "file", "locking" ]
What is the best way to open a file for exclusive access in Python?
186,202
<p>What is the most elegant way to solve this:</p> <ul> <li>open a file for reading, but only if it is not already opened for writing</li> <li>open a file for writing, but only if it is not already opened for reading or writing</li> </ul> <p>The built-in functions work like this</p> <pre><code>&gt;&gt;&gt; path = r"...
29
2008-10-09T06:58:54Z
21,444,311
<p>Assuming your Python interpreter, and the underlying os and filesystem treat os.rename as an atomic operation and it will error when the destination exists, the following method is free of race conditions. I'm using this in production on a linux machine. Requires no third party libs and is not os dependent, and as...
1
2014-01-29T22:45:22Z
[ "python", "file", "locking" ]
What is the best way to open a file for exclusive access in Python?
186,202
<p>What is the most elegant way to solve this:</p> <ul> <li>open a file for reading, but only if it is not already opened for writing</li> <li>open a file for writing, but only if it is not already opened for reading or writing</li> </ul> <p>The built-in functions work like this</p> <pre><code>&gt;&gt;&gt; path = r"...
29
2008-10-09T06:58:54Z
28,532,580
<p>EDIT: <strong>I solved it myself!</strong> By using <strong><em>directory existence</em></strong> &amp; age as a locking mechanism! Locking by file is safe only on Windows (because Linux silently overwrites), but locking by directory works perfectly both on Linux and Windows. See my GIT where I created an easy to us...
2
2015-02-15T23:41:16Z
[ "python", "file", "locking" ]
Python - How to use Conch to create a Virtual SSH server
186,316
<p>I'm looking at creating a server in python that I can run, and will work as an SSH server. This will then let different users login, and act as if they'd logged in normally, but only had access to one command.</p> <p>I want to do this so that I can have a system where I can add users to without having to create a s...
5
2008-10-09T07:54:12Z
186,465
<p>While Python really is my favorite language, I think you need not create you own server for this. When you look at the <a href="http://www.openbsd.org/cgi-bin/man.cgi?query=sshd&amp;sektion=8" rel="nofollow">OpenSSH Manualpage for sshd</a> you'll find the "command" options for the authorized keys file that lets you ...
-2
2008-10-09T09:00:11Z
[ "python", "twisted" ]
Python - How to use Conch to create a Virtual SSH server
186,316
<p>I'm looking at creating a server in python that I can run, and will work as an SSH server. This will then let different users login, and act as if they'd logged in normally, but only had access to one command.</p> <p>I want to do this so that I can have a system where I can add users to without having to create a s...
5
2008-10-09T07:54:12Z
189,452
<p>When you write a Conch server, you can control what happens when the client makes a shell request by implementing <a href="http://twistedmatrix.com/trac/browser/trunk/twisted/conch/interfaces.py?rev=24441#L62"><code>ISession.openShell</code></a>. The Conch server will request <a href="http://twistedmatrix.com/trac/...
7
2008-10-09T22:30:29Z
[ "python", "twisted" ]
'from X import a' versus 'import X; X.a'
186,472
<p>I've seen some Python programmers use the following style fairly consistently (we'll call it style 1):</p> <pre><code>import some_module # Use some_module.some_identifier in various places. </code></pre> <p>For support of this style, you can cite the <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow...
7
2008-10-09T09:04:34Z
186,483
<p>With the existence of the following syntax:</p> <pre><code>import some_other_module as some_module </code></pre> <p>the maintainability argument of style 2 is no longer relevant.</p> <p>I tend to use style 1. Normally, I find that I explicitly reference the imported package name only a few times in a typical Pyth...
5
2008-10-09T09:08:22Z
[ "python", "python-import", "maintainability", "duck-typing" ]
'from X import a' versus 'import X; X.a'
186,472
<p>I've seen some Python programmers use the following style fairly consistently (we'll call it style 1):</p> <pre><code>import some_module # Use some_module.some_identifier in various places. </code></pre> <p>For support of this style, you can cite the <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow...
7
2008-10-09T09:04:34Z
186,485
<p>I usually use a threshold to decide this. If I want to use a lot of things within <code>some_module</code>, I'll use:</p> <pre><code>import some_module as sm x = sm.whatever </code></pre> <p>If there's only one or two things I need:</p> <pre><code>from some_module import whatever x = whatever </code></pre> <p>T...
2
2008-10-09T09:09:49Z
[ "python", "python-import", "maintainability", "duck-typing" ]
'from X import a' versus 'import X; X.a'
186,472
<p>I've seen some Python programmers use the following style fairly consistently (we'll call it style 1):</p> <pre><code>import some_module # Use some_module.some_identifier in various places. </code></pre> <p>For support of this style, you can cite the <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow...
7
2008-10-09T09:04:34Z
186,486
<p>I believe in newer versions of Python (2.5+? must check my facts...) you can even do:</p> <pre><code>import some_other_module as some_module </code></pre> <p>So you could still go with style 1 and swap in a different module later on.</p> <p>I think it generally maps to how much you want to clutter up your namespa...
0
2008-10-09T09:10:18Z
[ "python", "python-import", "maintainability", "duck-typing" ]