Linked Questions

0 votes
1 answer
60 views

Which one is better to use in this example? Try-Except or If-Else? [duplicate]

I have just read some questions about when to use exception handling. And a question came up, from these 2, which one is better? def get_ans(q): while True: x = input(q) if x.isdigit(): ...
Darren's user avatar
  • 11
4481 votes
47 answers
6.7m views

How can I find the index for a given item in a list?

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index 1?
Eugene M's user avatar
  • 49.2k
343 votes
35 answers
416k views

What is the pythonic way to detect the last element in a 'for' loop?

How can I treat the last element of the input specially, when iterating with a for loop? In particular, if there is code that should only occur "between" elements (and not "after" ...
e.tadeu's user avatar
  • 5,367
381 votes
21 answers
1.1m views

"for line in..." results in UnicodeDecodeError: 'utf-8' codec can't decode byte

Here is my code, for line in open('u.item'): # Read each line Whenever I run this code it gives the following error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2892: ...
SujitS's user avatar
  • 11.5k
339 votes
25 answers
386k views

How to read a single character from the user?

Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like getch()). I know there's a function in Windows for ...
Evan Fosmark's user avatar
205 votes
11 answers
469k views

If list index exists, do X [duplicate]

I need to code such that if a certain list index exists, then run a function. My failing code: n = int(input("Define number of actors: ")) count = 0 nams = [] while count < n: count = ...
user1569317's user avatar
  • 2,495
155 votes
8 answers
26k views

How do "and" and "or" act with non-boolean values?

I'm trying to learn python and came across some code that is nice and short but doesn't totally make sense the context was: def fn(*args): return len(args) and max(args)-min(args) I get what it'...
Marcin's user avatar
  • 2,019
202 votes
3 answers
159k views

Conditional import of modules in Python [duplicate]

In my program I want to import simplejson or json based on OS being Linux or Windows. I take the OS name as input from the user. Now, is it correct to do it with a condition like this? osys = ...
Tim's user avatar
  • 2,061
83 votes
6 answers
128k views

How to overwrite a folder if it already exists when creating it with makedirs?

The following code allows me to create a directory if it does not already exist. dir = 'path_to_my_folder' if not os.path.exists(dir): os.makedirs(dir) The folder will be used by a program to ...
Shankar Kumar's user avatar
39 votes
8 answers
81k views

Upload files using SFTP in Python, but create directories if path doesn't exist

I want to upload a file on a remote server with Python. I'd like to check beforehand if the remote path is really existing, and if it isn't, to create it. In pseudocode: if(remote_path not exist): ...
franzlorenzon's user avatar
25 votes
10 answers
35k views

Python, Determine if a string should be converted into Int or Float

I want to convert a string to the tightest possible datatype: int or float. I have two strings: value1="0.80" #this needs to be a float value2="1.00" #this needs to be an integer. How I can ...
ManuParra's user avatar
  • 1,551
29 votes
12 answers
26k views

Python list.index throws exception when index not found

Why does list.index throw an exception, instead of using an arbitrary value (for example, -1)? What's the idea behind this? To me it looks cleaner to deal with special values, rather than exceptions. ...
Joan Venge's user avatar
  • 334k
29 votes
1 answer
32k views

Python Get Property if Object is not None

Is there a Pythonic way to grab a property if an object exists on one line of code? Currently in the code below if someone passes in a None type object the code will break, so I need some clean way to ...
CamJohnson26's user avatar
  • 1,219
13 votes
4 answers
32k views

What's the best way to check if class instance variable is set in Python?

I have a variable which may or may not get a value in the instance: class EC(): __init__(self, a=False): ... if a: self.__var = ... Later I want to check if the __var exists in the instance. ...
Phoenix's user avatar
  • 821
12 votes
3 answers
31k views

Simple DER Cert Parsing in python

Which is the best way to parse with python a binary file with X509 Certificate in DER format to extract public key.
David A's user avatar
  • 821

15 30 50 per page
1
2 3 4 5
9