Rajendra Dharmkar has Published 452 Articles

How to insert a Python object in MySQL?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Jan-2020 06:11:54

747 Views

Assuming that a MySQL database named 'test' is present on the server and a table named employee is also created. Let the table have five fields fname, lname, age, gender, and salary.Suppose we want to insert a tuple object containing data of a record defined as follows into the Msql ... Read More

How to match tab and newline but not space using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Dec-2019 09:03:45

1K+ Views

The following code matches tab and newline but not space from given string using regex.Exampleimport re print re.findall(r"[\t]","""I find     Tutorialspoint useful""")OutputThis gives the output['']

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Dec-2019 09:02:07

673 Views

The following code removes tabs and newlines from given stringExampleimport re print re.sub(r"\s+", " ", """I find Tutorialspoint helpful""")OutputThis gives outputI find Tutorialspoint helpful

How to compare two strings using regex in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Dec-2019 08:49:48

1K+ Views

We can compare given strings using the following codeExampleimport re s1 = 'Pink Forest' s2 = 'Pink Forrest' if bool(re.search(s1,s2))==True:    print 'Strings match' else:    print 'Strings do not match'OutputThis gives the outputStrings do not match

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Dec-2019 08:57:39

297 Views

The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']

How to copy files from one server to another using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Dec-2019 07:53:53

8K+ 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.For 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 ... Read More

How do I get the parent directory in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Dec-2019 06:23:38

1K+ Views

In Python 3.4+ you can use the pathlib module to get the parent directory.Examplefrom pathlib import Path print(Path('/home/username').parent)OutputThis will give the output:/homeIn older versions, you can call the os.path.join on your path and '..'(represents parent directory) and then find its absolute path using os.path.abspath.Exampleimport os print(os.path.abspath(os.path.join('/home/username', '..')))OutputThis will give the ... Read More

How to get a list of all sub-directories in the current directory using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Dec-2019 06:18:58

9K+ Views

To get a list of all subdirectories in a directory, recursively, you can use the os.walk function. It returns a three tuple with first entry being all the subdirectories.You can use it as follows:import os subdirs = [x[0] for x in os.walk('.')] print(subdirs)You can also list the directories(immediate only) using ... Read More

What is __init__.py in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Dec-2019 06:14:02

2K+ Views

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty ... Read More

How to call Python file from within PHP?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 10:47:39

9K+ Views

To call a Python file from within a PHP file, you need to call it using the shell_exec function.For exampleThis will call the script. But in your script at the top, you'll need to specify the interpreter as well. So in your py file, add the following line at the ... Read More

Advertisements