Categories
Python Answers

How to generate a random string generation with upper case letters and digits with Python?

Sometimes, we want to generate a random string generation with upper case letters and digits with Python.

In this article, we’ll look at how to generate a random string generation with upper case letters and digits with Python.

How to generate a random string generation with upper case letters and digits with Python?

To generate a random string generation with upper case letters and digits with Python, we can use the random.choices method.

For instance, we write

s = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))

to call random.choices with the list of characters we want to choose from that we get from

string.ascii_uppercase + string.digits

And then we set k to N characters to return N characters in the generated string.

We join the returned characters list into a string with join.

Conclusion

To generate a random string generation with upper case letters and digits with Python, we can use the random.choices method.

Categories
Python Answers

How to lock a file in Python?

Sometimes, we want to lock a file in Python.

In this article, we’ll look at how to lock a file in Python.

How to lock a file in Python?

To lock a file in Python, we can use the filelock library.

To install it, we run

pip install filelock

Then we use it by writing

from filelock import FileLock

with FileLock("myfile.txt"):
    # ...
    print("Lock acquired.")

to create FileLock object with the path to lock the file at the path that we passed into FileLock.

And then we run the code we want on the file with the file locked.

Then the file will be closed when everything is run since we used with when we lock the file.

Conclusion

To lock a file in Python, we can use the filelock library.

Categories
Python Answers

How to generate single executable file with Python py2exe?

Sometimes, we want to generate single executable file with Python py2exe.

In this article, we’ll look at how to generate single executable file with Python py2exe.

How to generate single executable file with Python py2exe?

To generate single executable file with Python py2exe, we can create a setup.py file that bundles all the files in the program in a file.

For instance, we write

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    windows = [{'script': "single.py"}],
    zipfile = None,
)

in setup.py to set the bundle_files dict entry to 1 and compressed to True to create a compressed bundle with the entry point being single.py.

Conclusion

To generate single executable file with Python py2exe, we can create a setup.py file that bundles all the files in the program in a file.

Categories
Python Answers

How to fix Python Requests throwing SSLError?

Sometimes, we want to fix Python Requests throwing SSLError.

In this article, we’ll look at how to fix Python Requests throwing SSLError.

How to fix Python Requests throwing SSLError?

To fix Python Requests throwing SSLError, we can set the verify argument.

For instance, we write

requests.get('https://example.com', verify=True)

to call requests.get with the URL and the verify argument set to True to verify the SSL certificate when making the GET request.

We can set verify to False to skip verification.

Conclusion

To fix Python Requests throwing SSLError, we can set the verify argument.

Categories
Python Answers

How to use raw_input in Python 3?

Sometimes, we want to use raw_input in Python 3.

In this article, we’ll look at how to use raw_input in Python 3.

How to use raw_input in Python 3?

To use raw_input in Python 3, we replace raw_input with input.

For instance, we write

print("Hi " + input("Say something: "))

to call input with a string for the prompt.

And then we get the entered value from the value returned from input.

Conclusion

To use raw_input in Python 3, we replace raw_input with input.