2,646 Posted Topics

Member Avatar for danholding

I think you should put the full path at line 37 in your code [code=python] shutil.move(path, dst_file) [/code] Also I think you should sort date_file_list only after the loop [icode]for file in glob.glob...[/icode].

Member Avatar for danholding
0
421
Member Avatar for _neo_

There was an old solution on linux to kill a subprocess [code=python] # linux, mac os.kill(pid, signal.SIGKILL) killedpid, stat = os.waitpid(pid, os.WNOHANG) if killedpid == 0: print >> sys.stderr, "ACK! PROCESS NOT KILLED?" [/code] however the [icode]Popen.terminate()[/icode] method is more recent and probably does about the same. Also what you …

Member Avatar for Gribouillis
0
1K
Member Avatar for sss33
Re: File

Your question is much too vague. You should describe the content of your file and the 'formula' that you want to apply. A general pattern to handle several lines of data is [code=python] def handle_line(line): pass # do whatever you want with a line here for line in open("myfile.txt"): handle_line(line) …

Member Avatar for Gribouillis
0
99
Member Avatar for ryuurei

Nice work. You may be interested in the python program [b]grin[/b] [url]http://pypi.python.org/pypi/grin[/url], which is multiplatform, but produces a result similar to unix' grep command. A nice feature of grin is that it uses python regular expressions in the query. Also its source code may contain ideas to improve your program.

Member Avatar for tls-005
3
453
Member Avatar for doomas10

You can iterate over the words, or build a list, with a regular expression (the [b]re[/b] module) [code=python] import re abstract = """hello, my name is george. How are you? Today i am not feeling very well. I consider myself to be sick.""" word_pattern = re.compile(r"\w+") print list(word_pattern.findall(abstract)) """ my …

Member Avatar for doomas10
0
301
Member Avatar for rahul8590

The Timer.timeit function runs your function 1 000 000 times by default, so your function runs in 21 microseconds on average. If you want to run a program only once and measure its performance, you should use the 'cProfile' or 'profile' module. See [url]http://docs.python.org/library/profile.html[/url].

Member Avatar for Beat_Slayer
0
849
Member Avatar for deeapk_kapdi

You should tell us what's the error on the command line (paste the console output), and also paste your program here, it would be easier to help you.

Member Avatar for TrustyTony
0
9K
Member Avatar for lewashby

It's not exactly true. xrange() is a builtin function in python 2 which returns an iterator instead of a list (range() returns a list). In python 3, range() returns an iterator, so python 3's range is python 2's xrange. In python 2, use xrange rather than range, because it's faster. …

Member Avatar for vegaseat
0
260
Member Avatar for teddypwns

Look at your function, when depth > 0, it executes function(depth -1), but it returns nothing (hence None). You must write [code=python] def function(depth): if depth > 0: return function(depth - 1) else: return 10 function(5) [/code]

Member Avatar for ultimatebuster
0
11K
Member Avatar for kuchi

Use a third dict [code=python] D = {'fstn':'Tim','lstn':'Parker','mdl':'W','dob':'19801201'} remap = {'fstn':'FIRST_NAME','lstn':'LAST_NAME','mdl':'MIDDLE_NAME','dob':'BIRTH_DAY'} otherD = dict((remap[key], value) for (key, value) in D.items()) print(otherD) """my output --> {'BIRTH_DAY': '19801201', 'FIRST_NAME': 'Tim', 'LAST_NAME': 'Parker', 'MIDDLE_NAME': 'W'} """ [/code]

Member Avatar for kuchi
0
360
Member Avatar for jtaylor-bye

This is a recurrent question in this forum: use the search form at the top of the page. Otherwise, read 'dive into python' online. You can also read the more than 200 entries in the thread 'starting python' above, and the same number of posts in 'projects for the beginner': …

Member Avatar for lrh9
0
138
Member Avatar for krishna_sun82

Try this perhaps [url]http://docs.python.org/extending/embedding.html#linking-requirements[/url] Otherwise, many people had the same problem, so check this here [url]http://lmgtfy.com/?q=undefined+symbol%3A+PyExc_OverflowError[/url]

Member Avatar for Gribouillis
0
49
Member Avatar for gonzigg

This program is 3000 lines long. It can easily be reduced to 500 lines by refactoring. I think this is the first step before you add new features.

Member Avatar for tbone2sk
0
343
Member Avatar for anurag.kyal
Member Avatar for Gribouillis
0
87
Member Avatar for j3nny

0xfe is not the hex of -2, it is the hex of 256 - 2. You can use [code=python] >>> hex(-2 % 256) '0xfe' >>> hex(256 - 2) '0xfe' [/code]

Member Avatar for jcao219
0
97
Member Avatar for gonzigg

[QUOTE=gonzigg;1296557]ok that worked just fine, but im trying to make a "who wants to be a millionaire" sort of game, so how can I use this example and turn it in to a window that has the question with 4 buttons, 3 of them will be wrong answer, and the …

Member Avatar for Gribouillis
0
235
Member Avatar for personx1212

I'm running python 2.6, and it does not raise an exception, instead, it converts path to unicode: [code=python] >>> path = 'C:\\Files\' >>> UnicodeName = u"\u2344" >>> path+=UnicodeName+'.txt' >>> path u'C:\\Files\\\u2344.txt' >>> print path C:\Files\⍄.txt [/code] There must be something missing in your example. Can you post console output ?

Member Avatar for personx1212
0
119
Member Avatar for Malinka

You can use os.walk this way [code=python] import os def must_open(dirpath, filename): """change this function to select if a file must be opened or not""" return True def opened_files(*args): """generate a sequence of pairs (path to file, opened file) in a given directory. Same arguments as os.walk.""" for dirpath, dirnames, …

Member Avatar for Gribouillis
0
9K
Member Avatar for nuaris

No, in python methods, there is no implicit argument (like C++'s this for example)

Member Avatar for yinchao
0
2K
Member Avatar for hisan

[QUOTE=woooee;1292184]That comes from the OS and depends on what "graphic card details" means. If you just want to know what type of graphics card, use subprocess to run lspci and pipe the output to a file. You can then read the file and search for "graphic".[/QUOTE] I like this command …

Member Avatar for Gribouillis
0
7K
Member Avatar for lionaneesh

With python 2, don't use input, use raw_input, which returns a string, and convert the result to an int. Here is a modified version which runs (but you didn't choose the fastest way to solve the problem :) ) [code=python] import sys if sys.version_info < (3,): input = raw_input array …

Member Avatar for TrustyTony
0
798
Member Avatar for krishna_sun82

There are several solutions to this problem. The simplest solution is to put dummy.py in a directory which appears in your PYTHONPATH environment variable. You can create a directory to routinely store your python modules and add this directory to your PYTHONPATH.

Member Avatar for krishna_sun82
1
1K
Member Avatar for Hawkpath

Write a program which uses matplotlib to trace a function like sqrt(abs(sin(x ** 2))). (See [url]http://matplotlib.sourceforge.net/gallery.html[/url]).

Member Avatar for Gribouillis
0
139
Member Avatar for sial_m

Try this command [code=python] python -c "import os, idlelib; execfile(os.path.join(os.path.split(idlelib.__file__)[0], 'idle.py'))"& [/code] It should be a multiplatform command. If you can't import idlelib, it means that you must install the tkinter package. Note that this simpler command also works [code=python] python -c "from idlelib import idle"& [/code]

Member Avatar for Gribouillis
0
353
Member Avatar for zyrus001

A nice alternative is to use a ZODB, or "Zope object database". In such a database, you can store persistent python objects and use them in very much the same way as you handle ordinary python objects. You don't need Zope to install and use ZODB: they form a standalone …

Member Avatar for griswolf
0
321
Member Avatar for Peter_TARAS

The simplest idea would be to give your instance a member 'name' and pass this name when you create the instance, for example [code=python] class MyClass(object): def __init__(self, name, *args): self.name = name my_instance = MyClass("GuidoVanRossum") [/code]

Member Avatar for vegaseat
0
17K
Member Avatar for abcdr

You can use a recursive auxiliary function to count an element [code=python] def count(element): if isinstance(element, str): return len(element) else: return sum(count(x) for x in element) def countEachElement(): elements= ["spam!",'1',['Brie','Roquefort','Pol le Veq'],['1','2','3']] return count(elements) print(countEachElement()) """ my output --> 32 """ [/code] Remarks: 1) PLEASE, configure your editor to insert …

Member Avatar for jcao219
0
100
Member Avatar for gonzigg

Another version [code=python] from graphics import * def item_n_price(win, i, items, prices): z = 50 * i center = Point(50,50+z) label = Text(center, "Item >>") label.draw(win) input = Entry(Point(170,55+z), 20) input.setText("item") input.draw(win) items.append(input) center = Point(350,50+z) label = Text(center, "Price >>") label.draw(win) input = Entry(Point(420,55+z), 6) input.setText("0.00") input.draw(win) prices.append(input) def …

Member Avatar for gonzigg
0
4K
Member Avatar for cmalkmes

I would rather write it this way, using a multiline template and the string % operator [code=python] # Script to iterate N,S,E,W coordinates of extent rectangles in 60x60 grid # and write iterations of 20 lines of code to Region.kml # IMPORT ARCGISSCRIPTING MODULE import arcgisscripting, os template = """ …

Member Avatar for cmalkmes
0
266
Member Avatar for Jothe

A good way to learn python is to read 'dive into python' [url]http://diveintopython.org/[/url]. Note that there are a few incompatibilities between python 2 (up to version 2.7) and python 3 (starting with version 3.0), so you may prefer to read 'dive into python 3' [url]http://diveintopython3.org/[/url].

Member Avatar for anurag mishra
0
109
Member Avatar for pendo19

A simple way is this [code=python] import Tkinter as tk # gives tk namespace import csv def hello(): # etc def get_list(event): # etc def main(): # put line 26 to 141 in this function if __name__ == "__main__": # this will not be executed if your file is imported …

Member Avatar for pendo19
0
234
Member Avatar for pythonbegin

Try this code, which uses list comprehensions [code=python] from pprint import pprint def records(filename): """generates pairs (word, line) from the file, where word is the first column""" return ((line[:line.find('\t')], line) for line in open(filename)) L1 = list(records('testfile1.txt')) D1 = dict(L1) assert(len(L1) == len(D1)) # check that keys are unique in …

Member Avatar for TrustyTony
0
775
Member Avatar for spac_e

You should create a list for your owner_id: [code=python] class sendbackcurricula(app.page): def GET(self): query = models.Curricula.query.all() map = dict() for x in query: x = x.to_dict() key, title = x['owner_id'], x['title'] if key not in map: map[key] = list() map[key].append(title) return json.dumps(map) [/code] This should return {"email": ["Course1", "Course2", "Course3"] …

Member Avatar for TrustyTony
0
80
Member Avatar for kimberling

I suggest this [code=python] from random import randrange def randhex(hexdigits): s = hex(randrange(16 ** hexdigits)) if s[-1] == 'L': s = s[:-1] return s for i in range(10): print(randhex(8)) """ my output --> 0xae662345 0xd2b915f5 0x244435bd 0xdf1c08e 0x73d6018f 0xf4b05b97 0x589e2a82 0xad3a2ea9 0xea758570 0xbe48582d [/code]

Member Avatar for calccrypto
0
91
Member Avatar for Gribouillis

How would you safely round a floating point number to the nearest integer ? Python has the built in function [icode]round[/icode], but it returns a floating point number [code=python] >>> round(4.9) 5.0 >>> help(round) Help on built-in function round in module __builtin__: round(...) round(number[, ndigits]) -> floating point number Round …

Member Avatar for Gribouillis
0
3K
Member Avatar for personx1212

If your OS is linux, you should be able to store your file in /dev/shm which is a virtual directory which exists only in shared memory. Example [code=python] >>> f = open("/dev/shm/foo.txt", "w") >>> f.write("hello world\n") >>> f.close() >>> f = open("/dev/shm/foo.txt", "r") >>> print f.read() hello world >>> f.close() …

Member Avatar for personx1212
0
79
Member Avatar for acrocephalus

Also check this google code project [url]http://code.google.com/p/prettytable/[/url] .

Member Avatar for Gribouillis
0
2K
Member Avatar for spockGirl

I think you should try [b]matplotlib[/b]. See this code snippet [url]http://www.daniweb.com/code/snippet216915.html[/url] and other examples here [url]http://matplotlib.sourceforge.net/[/url] (especially the gallery [url]http://matplotlib.sourceforge.net/gallery.html[/url]).

Member Avatar for Gribouillis
0
132
Member Avatar for jonnyflash

After this call [icode]PyObject* pModule = PyImport_Import(pName);[/icode], you should at least check if pModule is NULL, meaning that the import failed.

Member Avatar for Gribouillis
0
1K
Member Avatar for hamstes

Currently, the default python in major linux distributions is python 2.6. If you installed python 3, you can probably invoke it with /usr/bin/python3 instead of /usr/bin/python. Make sure you install tkinter3 too (if you want to use idle for example). Don't try to remove or change your default python installation.

Member Avatar for redyugi
0
131
Member Avatar for aniyishay

Here is a little refactoring [code=python] # # Tic Tac Toe # import random # # This is the simple game board, (Just visualize a tic tac toe board) # game = list("012345678") # # 1. Marks a field (if it is a digit, otherwise an 'x' or 'o' is …

Member Avatar for aniyishay
0
2K
Member Avatar for Tommymac501

Start improving your code step by step. A few things come in mind: 1) Each time code is repeated in a program, repetition should be removed by a loop or by defining functions. Examples are lines 129-140 and 169-174. The variables O1, w1, ..., O6, w6 should be stored in …

Member Avatar for Gribouillis
0
175
Member Avatar for ultimatebuster

[QUOTE=ultimatebuster;1279722]I know about re-raising, but then it would still terminate the program.[/QUOTE] If your program encounters an unexpected exception, the best thing to do is usually to terminate the program, because your program's data were modified in an unpredictable way. It is not a universal rule however. For example, if …

Member Avatar for Gribouillis
0
213
Member Avatar for ganeshredcobra

[code=python] openfile = open('/home/space/Desktop/test.txt', 'r') for line in openfile: if "error" in line: print(line) break [/code]

Member Avatar for TrustyTony
0
149
Member Avatar for acrocephalus

You can also do this [code=python] print w.fill('WIP will create Backup, Resized and Watermarked folders to store the original, resized and watermarked pictures. It will overwrite them if they already exists. Are you sure you want to continue (y/n)?') overwrite = raw_input('> ') while overwrite not in ('y', 'n'): overwrite …

Member Avatar for TrustyTony
0
141
Member Avatar for abbyo

You can try to run this program [code=python] import xlrd import xlwt import xlutils [/code] If it doesn't run, it means you haven't installed the programs correctly. If it runs, it most likely means that you have installed them correctly.

Member Avatar for ultimatebuster
0
103
Member Avatar for technocratic

First avoid 'list' as a variable name since it is the name of a builtin type. There are at least 2 ways to do it [code=python] old_list = ["a","b","c","d"] new_list = list() for x in old_list: new_list.append(ord(x)) [/code] or, using 'list comprehension syntax' [code=python] old_list = ["a","b","c","d"] new_list = [ …

Member Avatar for TrustyTony
0
3K
Member Avatar for kumarantechie

If you have multiple installations of python of your machine (say 2.6 and 2.5) and you are running the program with python 2.5, you may have to install a version of pygtk for your python 2.5. See this entry in the pygtk FAQ [url]http://faq.pygtk.org/index.py?req=show&file=faq01.010.htp[/url]

Member Avatar for kumarantechie
0
945
Member Avatar for WildBamaBoy

You can use a list [code=python] FrameFolders = [(modsDir,"Mods"), (Packages,"Packages"), (DCCache,"DCCache")] for path, name in FrameFolders: if not os.path.isdir(path): Ok = False print "%s folder does not exist. Creating..." % name #Just using % path prints the ENTIRE directory name os.mkdir(path) if os.path.isdir(path): print "Folder %s sucessfully created.\n" % name …

Member Avatar for TrustyTony
0
113
Member Avatar for mtliendo

The problem is that [icode]raw_input[/icode] returns a string and not an integer. You must convert it to integer with the [icode]int[/icode] function. See this example [code] >>> x = raw_input("enter a number: ") enter a number: 2 >>> print x, repr(x), type(x) 2 '2' <type 'str'> >>> x = int(x) …

Member Avatar for griswolf
0
174

The End.