function – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sat, 31 Dec 2022 18:06:24 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Read Text Files in Python https://tecadmin.net/how-to-read-text-files-in-python/ https://tecadmin.net/how-to-read-text-files-in-python/#respond Wed, 28 Dec 2022 09:37:14 +0000 https://tecadmin.net/?p=33034 While working with the Python application, you would be required to read and write text files in Python. You can refer to our other tutorial to write a text file in Python. Reading a text file in Python is a simple process that can be accomplished using a few different methods. In this article, we [...]

The post How to Read Text Files in Python appeared first on TecAdmin.

]]>
While working with the Python application, you would be required to read and write text files in Python. You can refer to our other tutorial to write a text file in Python. Reading a text file in Python is a simple process that can be accomplished using a few different methods.

In this article, we will cover the following methods for reading a text file in Python:

  • Using the `open()` function and `.read()` method
  • Using the `open()` function and `.readlines()` method
  • Using the `with` statement and `.read()` method
  • Using the `with` statement and `.readlines()` method

You can choose anyone the given methods based on your application scenario and environment. In this tutorial, I will read `myfile.txt` available in current directory that contains the following text:

cat myfile.txt 
Output:
Hi I'm Rahul Welcome you on tecadmin.net

Let’s take a closer look at each of these methods one by one.

Method 1: Using the `open()` function and `.read()` method

The first method for reading a text file in Python uses the `open()` function and the `.read()` method. Here is an example of how to use this method:

# Open the text file in read mode
file = open('myfile.txt', 'r')

# Read the contents of the file into a variable
contents = file.read()

# Print contents value
print(contents)

# Close the file
file.close()

Output:
Hi I'm Rahul Welcome you on tecadmin.net

In this example, we use the `open()` function to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). Then, we use the `.read()` method to read the contents of the file into a variable called contents. Finally, we close the file using the .close() method.

Method 2: Using the `open()` function and `.readlines()` method

The second method for reading a text file in Python involves using the `open()` function and the `.readlines()` method. This method is similar to the first method, but it returns a list of strings, where each string represents a line in the text file. Here is an example of how to use this method:

# Open the text file in read mode
file = open('myfile.txt', 'r')

# Read the contents of the file into a list of strings
lines = file.readlines()

# Print the lines
print(lines)

# Close the file
file.close()

Output:
['Hi\n', "I'm Rahul\n", 'Welcome you on tecadmin.net\n']

In this example, we use the `open()` function to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). Then, we use the `.readlines()` method to read the contents of the file into a list of strings called lines. Finally, we close the file using the .close() method.

Method 3: Using the `with` statement and `.read()` method

The third method for reading a text file in Python involves using the with the statement and the `.read()` method. This method is similar to the first method, but it automatically closes the file after the block of code within the `with` statement has been executed. Here is an example of how to use this method:

# Open the text file in read mode using the with statement
with open('myfile.txt', 'r') as file:
    # Read the contents of the file into a variable
    contents = file.read()
    print(contents)

Output:
Hi I'm Rahul Welcome you on tecadmin.net

In this example, we use the `with` statement to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). The `with` statement automatically closes the file after the block of code within the `with` statement has been executed.

Method 4: Using the `with` statement and `.readlines()` method

The fourth method for reading a text file in Python involves using the `with` statement and the `.readlines()` method. This method is similar to the second method, but it automatically closes the file after the block of code within the `with` statement has been executed. Here is an example of how to use this method:

# Open the text file in read mode using the with statement
with open('myfile.txt', 'r') as file:
    # Read the contents of the file into a list of strings
    lines = file.readlines()
    print(lines)

Output:
['Hi\n', "I'm Rahul\n", 'Welcome you on tecadmin.net\n']

In this example, we use the `with` statement to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). The `with` statement automatically closes the file after the block of code within the `with` statement has been executed. Within the `with` statement, we use the `.readlines()` method to read the contents of the file into a list of strings called lines.

Conclusion

In this article, we have covered four different methods for reading a text file in Python: using the `open()` function and `.read()` method, using the `open()` function and `.readlines()` method, using the `with` statement and `.read()` method, and using the `with` statement and `.readlines()` method. Each of these methods has its own advantages and disadvantages, and the best method to use will depend on your specific needs.

The post How to Read Text Files in Python appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-read-text-files-in-python/feed/ 0
How to Generate Random String in Python https://tecadmin.net/how-to-generate-random-string-in-python/ https://tecadmin.net/how-to-generate-random-string-in-python/#respond Tue, 27 Dec 2022 09:15:15 +0000 https://tecadmin.net/?p=33013 Generating random strings in Python is a common task that can be useful in various scenarios, such as when you need to create unique identifiers, when you want to generate random passwords, or when you want to create random data for testing purposes. In Python, you can use the random module and the string module [...]

The post How to Generate Random String in Python appeared first on TecAdmin.

]]>
Generating random strings in Python is a common task that can be useful in various scenarios, such as when you need to create unique identifiers, when you want to generate random passwords, or when you want to create random data for testing purposes. In Python, you can use the random module and the string module to generate random strings. The random module provides functions for generating random numbers, and the string module provides functions for generating random strings.

In this article, we will explore how to generate random strings in Python using these two modules.

Method 1: Using Python `secrets` Module

Here’s an example of how to use the `secrets` module to generate a random string in Python:

import secrets

random_string = secrets.token_hex(16)
print(random_string)

This code will generate a random string of 32 characters, consisting of hexadecimal digits (0-9 and a-f). The `token_hex()` function generates a secure, random string of the specified length using the `urandom()` function from the os module.

Method 1: Using Python `random` Module

You can also use the `random` module from the Python standard library to generate a random string. Here’s an example of how to do that:

import random
import string

def generate_random_string(length):
    return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))

random_string = generate_random_string(16)
print(random_string)

This code will generate a random string of 16 characters, consisting of uppercase and lowercase ASCII letters and digits. The `generate_random_string()` function uses the `random.choice()` function to randomly select characters from the string.ascii_letters and string.digits strings, and then it uses the `join()` function to combine them into a single string.

Conclusion

In conclusion, generating random strings in Python is a useful task that can be easily accomplished using the random module and the string module. The `random` module provides functions for generating random numbers, and the string module provides functions for generating random strings. By combining these two modules, you can generate random strings of any length and complexity. Understanding how to generate random strings in Python can be helpful in various scenarios, such as when you need to create unique identifiers, when you want to generate random passwords, or when you want to create random data for testing purposes.

I hope this helps! Let me know if you have any questions.

The post How to Generate Random String in Python appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-generate-random-string-in-python/feed/ 0
How to Call JavaScript Function on Click Events https://tecadmin.net/call-javascript-function-on-click-events/ https://tecadmin.net/call-javascript-function-on-click-events/#comments Tue, 20 Oct 2015 06:16:35 +0000 https://tecadmin.net/?p=8840 JavaScript is one of the core programming language used on the websites. JavaScript enables interactive web pages and is an essential part of web applications. Most of the modern websites relies on JavaScript to handle client side application behavior. All the web browsers have a dedicated JavaScript engine to execute it. In this tutorial you [...]

The post How to Call JavaScript Function on Click Events appeared first on TecAdmin.

]]>
JavaScript is one of the core programming language used on the websites. JavaScript enables interactive web pages and is an essential part of web applications. Most of the modern websites relies on JavaScript to handle client side application behavior. All the web browsers have a dedicated JavaScript engine to execute it.

In this tutorial you will learn, how to call a JavaScript function on various page events like: On click , on form submission, or other various HTML controls.

Create JavaScript Function

First of all, create a JavaScript function to use further in this tutorial. Add this JavaScript code between … tag of the web page. You may also create function under separate JavaScript file and include in HTML page.

<script type="text/javascript">
  function myFunction() {
	var dt = new Date();
	alert(dt);
}
</script>

The above function will print current datetime in a popup. Let’s call this function on click events of various HTML controls. These HTML controls needs to put under tag of web page.

1. Call JavaScript on Hyperlink Click

Add below HREF html code in body section and access on web browser. Now just click link, It will execute JavaScript’s myFunction which will show current date as per used above.

<a href="javascript:void(0);" onclick="myFunction();"> click here </a>

Call js function on link click

2. Call JavaScript on Form Submit

Next, create a sample form to call JavaScript function of form submit event. This is useful for validating form input values and then submit.

<form id="myForm" method="post">
     Name:    <input name="name" id="name" type="text" /><br />
    <input type="button" id="form1" onclick="myFunction();" value="Submit" />
</form>

Call JavaScript function on Form Submit

3. Call JavaScript on Button Click

Now call JavaScript function of button click event. Add the below code between the body tag of html page. This can be help ful to check form value or verify form values.

<button onclick="myFunction();">click here </button>

Call JavaScript function on Button Click

4. Call JavaScript on Link with P Tag

Now call JavaScript function link created using html “p” tag.

<p id="demo" onclick="myFunction()">click here </p>

Call JavaScript function on Link with P Tag

The post How to Call JavaScript Function on Click Events appeared first on TecAdmin.

]]>
https://tecadmin.net/call-javascript-function-on-click-events/feed/ 1