Rajendra Dharmkar has Published 452 Articles

How to print complete TimeTuple in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 05:28:39

131 Views

The timetuple() method of datetime.date instances returns an object of type time.struct_time. The struct_time is a named tuple object (A named tuple object has attributes that can be accessed by an index or by name).  exampleYou can print the complete time tuple by simply passing it to print.import datetime todaysDate = ... Read More

What is a Tick in python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 05:24:39

2K+ Views

The floating-point numbers in units of seconds for time interval are indicated by Tick in python. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch). You can use the time module to use functions for working with times, and for converting between representations. For example, if ... Read More

What are character classes or character sets used in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 10:49:31

2K+ Views

Character classA "character class", or a "character set", is a set of characters put in square brackets. The regex engine matches only one out of several characters in the character class or character set. We place the characters we want to match between square brackets. If you want to match ... Read More

How to add space before and after specific character using regex in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 10:45:44

1K+ Views

The following code shows how space is added before and after the pipe '|' character in given string. Exampleimport re regex = r'\b[|:]\b' s = "abracadabra abraca|dabara | abra cadabra abra ca dabra abra ca dabra abra" print(re.sub(regex, ' \g ', s))OutputThis gives the outputabracadabra abraca | dabara | abra cadabra ... Read More

What is the groups() method in regular expressions in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 10:40:57

10K+ Views

The re.groups() methodThis method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.  In later versions (from 1.5.1 on), a ... Read More

How to perform different commands over ssh with Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 07:15:27

1K+ Views

The simplest way to use SSH using python is to use paramiko. You can install it using −$ pip install paramikoTo use paramiko, ensure that you have correctly set up SSH keys(https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html) on the host machine and when running the python script, these keys are accessible. Once that is done ... Read More

How to use FTP in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 07:14:33

282 Views

You can use the ftplib module in Python. It allows you to write programs that perform a variety of automated FTP jobs. You can easily connect to a FTP server to retrieve files and process them locally.exampleimport ftplib ftp = ftplib.FTP('ftp.yourserver.com', 'yourusername', 'your@email.address') print "File List: " files = ftp.dir() ... Read More

How to copy a file to a remote server in Python using SCP or SSH?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 07:10:47

13K+ Views

The easiest way to copy files from one server to another over ssh is to use the scp command. For calling scp you'd need the subprocess module. exampleimport subprocess p = subprocess.Popen(["scp", "my_file.txt", "username@server:path"]) sts = os.waitpid(p.pid, 0)You need the waitpid call to wait for the copying to complete.Another solution is ... Read More

How to touch all the files recursively using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 05:55:42

649 Views

To touch all the  files recursively, you need to walk the directory tree using os.walk and add touch all the files in it using os.utime(path_to_file). exampleimport os # Recursively walk the tree for root, dirs, files in os.walk(path):     for file in files:         # Set utime ... Read More

How to create a unique directory name using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 04:56:12

815 Views

You can use the tempfile module to create a unique temporary directory in the most secure manner possible. There are no race conditions in the directory’s creation. The directory is readable, writable and searchable only by the creating user ID. Note that the user of mkdtemp() is responsible for deleting ... Read More

Advertisements