Categories
Python Answers

How to execute a program or call a system command with Python?

Sometimes, we want to execute a program or call a system command with Python.

In this article, we’ll look at how to execute a program or call a system command with Python.

How to execute a program or call a system command with Python?

To execute a program or call a system command with Python, we can use the subprocess.run method.

For instance, we write:

import subprocess
subprocess.run(["ls", "-l"])

We pass in an array with the command as the first entry.

And the options are in the subsequent entries.

Now we should see the listings of the content of the current working directory displayed.

Conclusion

To execute a program or call a system command with Python, we can use the subprocess.run method.

Categories
Python Answers

How to merge two dictionaries in a single expression with Python?

Sometimes, we want to merge two dictionaries in a single expression with Python.

In this article, we’ll look at how to merge two dictionaries in a single expression with Python.

How to merge two dictionaries in a single expression with Python?

To merge two dictionaries in a single expression with Python, we can use the ** or | operators.

For instance, we write:

x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}
print(z)

Then z is {'a': 1, 'b': 3, 'c': 4}.

** is available since Python 3.5.

We can also use the | operator with Python 3.9 or later.

To use it, we write:

x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = x | y

And we get the same value for z.

Conclusion

To merge two dictionaries in a single expression with Python, we can use the ** or | operators.

Categories
Python Answers

How to check whether a file exists without exceptions with Python?

Sometimes, we want to check whether a file exists without exceptions with Python.

In this article, we’ll look at how to check whether a file exists without exceptions with Python.

How to check whether a file exists without exceptions with Python?

To check whether a file exists without exceptions with Python, we can use the os.path.isFile method.

For instance, we write:

import os.path
fname = './foo.txt'
os.path.isfile(fname) 

fname is a file path string.

If the file doesn’t exist at the fname path, then it returns False.

Conclusion

To check whether a file exists without exceptions with Python, we can use the os.path.isFile method.

Categories
Python Answers

How to add a ternary conditional expression with Python?

Sometimes, we want to add a ternary conditional expression with Python.

In this article, we’ll look at how to add a ternary conditional expression with Python.

How to add a ternary conditional expression with Python?

To add a ternary conditional expression with Python, we can following the following format:

a if condition else b

where a and b are expressions.

For instance, if we have:

'true' if True else 'false'

Then true is returned.

Conclusion

To add a ternary conditional expression with Python, we can following the following format:

a if condition else b
Categories
JavaScript Answers

How to output text based on user’s current time with JavaScript?

Sometimes, we want to output text based on user’s current time with JavaScript.

In this article, we’ll look at how to output text based on user’s current time with JavaScript.

How to output text based on user’s current time with JavaScript?

To output text based on user’s current time with JavaScript, we can use the JavaScript date’s getHours method to get the hour of the day.

Then we can use an if statement to output text according to the hour of the day.

For instance, we write:

const today = new Date()
const curHr = today.getHours()

if (curHr < 12) {
  console.log('good morning')
} else if (curHr < 18) {
  console.log('good afternoon')
} else {
  console.log('good evening')
}

We get the current date and time with new Date().

Then we get the hour of the current date and time with getHours.

Finally, we use an if-else statement to check for the hour.

If it’s less than 12, then it’s morning.

If it’s less than 18, then it’s afternoon.

Otherwise, it’s evening.

Conclusion

To output text based on user’s current time with JavaScript, we can use the JavaScript date’s getHours method to get the hour of the day.

Then we can use an if statement to output text according to the hour of the day.