Skip to main content

Posts

Showing posts from April, 2018

Python's lists: append vs extend methods

Append vs Extend Let's discover how to change lists. What are lists in Python? Lists are one of the fondamental tools of Python. They are what arrays are for other languages. You can create a list simply like this: mylist = [1,2,3] But you can put everything in a list: strings, objects... anything. names_list = ["John", "Peter", "Mary"] There are different ways to add elements to a list. Let's take a look at  them. Two different methods... [caption id="" align="alignnone" width="715"] extend vs append[/caption] With append you can append a single element that will extend the list: >>> a = [ 1 , 2 ] >>> a . append ( 3 ) >>> a [ 1 , 2 , 3 ] If you want to extend more than one element you should use extend, because you can only append one elment or one list of element: >>> a . append ([ 4 , 5 ]) >>> a >>> [ 1 , 2 , 3 ,[ 4 , 5 ]] So ...

Find all the files in a directory

Find all the files in a directory There are a lot of way to do it, let’s start with the first. Using os.listdir() Italiano BasterĂ  importare il modulo os (import os) e poi usare la funzione os.listdir() (stampandola sulla console con print, per esempio). Inglese You just have to import the os module and you can use the os.listdir method (showing it on the screen with print, for example). >>>  import  os >>>  print (os. listdir ()) All the other ways… This has been taken from my stackoverflow answer… Get a list with the files I have made also a short video here:  Video os.listdir(): get files in current dir (Python 3) The simplest way to have the file in the current dir in Python 3 is this. It’s really simple, use the os module and the listdir() function and you’ll have the file in that dir (and eventual folders that are in the dir, but you will not have the file in the subdirectory, for that you can use walk – ...

Open a web page with Python

Open a web page with Python To do this we will use the webbrowser module. This code will open any link with Chrome. You will have to change the path to your browser installation. import webbrowser chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s" link = "www.google.com" webbrowser.get(chrome).open(link)