Can anyone tell me why the file content disappears whenever I try to save my modifications in Python?

In my file test_final.txt, I have a collection of simple words and sentences that I want to shuffle using the shuffle method. However, when I implement the code, it empties out my test_final.txt. Additionally, I am struggling to understand why the Shuffle method is not working as expected.

import random
import re 
import sys
f = open("test_final.txt", "r")
print(f.read())

pattern = r"([a-zA-Z]+) (\d+)"
listOfWords = re.findall(pattern, 'f')
print("Result: ", listOfWords)

random.Random(4).shuffle(listOfWords)
print("Shuffled stuff: ", listOfWords)


finalString = ""
for word in listOfWords: 
    finalString = finalString + word + " "
finalString = finalString.strip()
print(finalString)

#Save

with open("test_final.txt", "w", encoding = 'utf-8') as file:

   file.write(finalString)
  • For instance, if my test_final.txt contains:

my book is still here, the output after shuffling should be something like this still book here is my.

  • Despite noticing that the issue lies within the save method causing the file to become empty, I am also facing difficulty with the Shuffle method not shuffling the words correctly as desired.

Answer №1

There are several issues with your code.

print(f.read())

This line prints the contents to standard output and leaves the file handle at the end of the file. If you try to read anything more from f at this point, you will receive an empty string.

Since you are not storing the lines or generating new ones in the following code (as seen below), it makes sense that nothing is being written to the new file in the end.

mama = r"([a-zA-Z]+) (\d+)"
listOfWords = re.findall(mama, 'f')

This snippet searches for words followed by a space and numbers in the static string 'f', which obviously doesn't contain any spaces or numbers, resulting in the list listOfWords being empty. Were you attempting to read from the variable f instead? However, even if you were, note that findall expects a string as its second argument, not a file handle.

As mentioned by others, leaving f open while overwriting the same file won't work reliably. It's essential to close f before writing to the same file.

The purpose of calling random.Random(4) is unclear, so I'll assume you intended to shuffle the list randomly using an even distribution.

To achieve your goal of having the list of words as separate strings from the file, your current code needs modifications. Simply split the line and add it to your list.

For a potential solution, consider the following revised code snippet:

import random
import re

mama = r"[a-zA-Z]+\d*"

list_of_words = []

with open("test_final.txt", "r", encoding="utf-8") as f:
    for line in f:
        for word in line.rstrip('\n').split():
            if re.match(mama, word):
                list_of_words.append(word)
            else:
                print('# discarding', word)

print("Result: ", list_of_words)

random.shuffle(list_of_words)
print("Shuffled stuff: ", list_of_words)

final_string = " ".join(list_of_words)
print(final_string)

with open("test_final.txt", "w", encoding='utf-8') as file:
   file.write(final_string)

See a demonstration here: https://ideone.com/fBXb8v

Note that the provided code rearranges the file content onto a single line separated by spaces. This could potentially be destructive, so consider keeping each original line on a separate line for stability and repeatability.

Answer №2

In order to save the changes, it is important to properly close the file.

closeFile()

Answer №3

When starting the script, you should open a file and read its contents. Make sure to properly close it afterwards. Consider using the with statement to handle file opening.

import random
import re 
import sys
with open("test_final.txt", "r", encoding = 'utf-8') as f:
    text = f.read()
    print(text)

pattern = r"([a-zA-Z]+) (\d+)"
list_of_words = re.findall(pattern, 'f')
print("Result: ", list_of_words)

random.Random(4).shuffle(list_of_words)
print("Shuffled content: ", list_of_words)

final_string = ""
for word in list_of_words: 
    final_string = final_string + word + " "
final_string = final_string.strip()
print(final_string)

# Save

with open("test_final.txt", "w", encoding = 'utf-8') as f:
   f.write(final_string)

Answer №4

Attempting to both read and write the same file concurrently is not a viable option. When you open a file for writing, it will erase all existing data in the file and start fresh. You can choose to open a file in append mode if you want new data to be added at the end of the file, but this might not align with your intentions. Typically, if you need to modify a file, it's best to create a new one and then replace the old file with the new version if desired.

Additionally, remember to close the file after you finish reading from it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Send the data from a table to a Python method in Flask using AJAX

Recently, I picked up on flask and decided to create a small web tool for organizing data. The main page displays a table of list numbers and their respective descriptions. I managed to implement jQuery functionality that allows double clicking to select i ...

"Encountered an issue with locating elements using the driver.find_element

I need assistance in automating the process of clicking on the "Create New Network" button using Selenium. <button type="button" id="dt-refreshBtn" class="btn wc-btn--link" data-label="Create New Network" role=&q ...

CORS blocked the HTTP request due to the absence of the Access-Control-Allow-Origin header, although the header is actually present

Recently, I encountered an issue while working on a project using Python with Flask. I had created a REST API and needed my Vue app to interact with it and fetch data. However, I kept receiving an error message stating "No 'Access-Control-Allow-Origin ...

Select the list item containing the desired text by clicking on it

When trying to click on a specific element based on the text displayed on this page using Python 3 and Chrome driver, I encountered an issue. For instance, when searching for "BEBES", I used the following code: WebDriverWait(browser, 10).until(EC.element_ ...

Issue with Selenium failing to clear search bar using .clear() method

When scraping a website, I noticed that the search bar auto-populates with the last search query. Even after using .clear(), the search bar still retains the previously searched item. for sku in skus_to_find: search_bar = WebDriverWait(driver,100).unti ...

Error: Unable to locate the module titled 'bs4'. The module cannot be utilized at this time

Hello everyone! I'm a beginner in Python and currently using Python 3.6.4 (64-bit). I recently installed pandas and matplotlib successfully, but I'm facing difficulties importing bs4. Can someone please provide guidance on how to resolve this is ...

Is Openshift compatible with selenium testing tools?

Can Openshift support GUI applications like Selenium? I am looking to deploy my Python application in the cloud... If not, is there a way to work around this issue? The application needs to log into a website and perform tasks for a few seconds.. ...

Basic linear regression with a limitation

After developing an algorithm to iterate through 15 variables and generate a basic OLS for each one, I then expanded the algorithm to loop another 11 times. This time, it produced the same 15 OLS regressions, but with the lag of the X variable increasing b ...

What could be causing the DataFrame replace function to not work as expected?

I am currently working on a task to fill in missing values (NaN) in the train_df dataframe by using corresponding index values from the dff dataframe. However, something seems to be off and I can't quite figure out where I am going wrong. train_df.rep ...

Creating a Python script to generate a 3D plot using multiple dataframes

If I have three different DataFrames in Python using pandas library: df_sales = pd.DataFrame([[20,30,10], [30,20,20], [20,40,40]], columns=list("ABC")) A B C 0 20 30 10 1 30 20 20 2 20 40 40 df_people_info = pd.DataFrame([[2,3,1], [3 ...

Incorporating "quad" and "quadrature" into Python/SciPy for seamless integration

Upon reviewing the documentation on this and that, I noticed that both "quad" and "quadrature" could potentially be used interchangeably in terms of syntax. However, it appears that they are not entirely interchangeable: from scipy.integrate import quad a ...

Guide: Generating a Random Number with Prefix using Python

Can you help me create a list of all potential numbers in the given prefix? import random prefix = "05" print prefix + #List of Potential Numbers Goes Here ...

Guide for including Publisher Certificate in Cx_freeze msi package

While creating an "msi" using cx_freeze, I am encountering a problem where the distributed file is showing unknown publisher. What are the steps to obtain publisher certificates and how can they be added to cx_freeze? ...

Is there a way to utilize in Python to have the text printed on the same line?

Could someone provide a detailed explanation of how the '\r' character functions in Python? I'm curious why the code below isn't displaying any output on the screen. #!/usr/bin/python from time import sleep for x in range(10000) ...

Python - exec - retrieving a specific data point

Currently, I am attempting to extract specific information from a lengthy string formatted as "text". My focus is on obtaining the values of "shade": >>> text = 'colors = {\r\n\r\n "1234": {\r\n ...

Looking to iterate through a dataframe and transform each row into a JSON object?

In the process of developing a function to transmit data to a remote server, I have come across a challenge. My current approach involves utilizing the pandas library to read and convert CSV file data into a dataframe. The next step is to iterate through t ...

From Panda's Den to JSON Empire: Unraveling the Dataframe

After an exhaustive review and attempt at implementing all the other solutions on SO related to this challenge, I have yet to find a working solution. Question: How can I convert employee and supervisor pairs into a dynamic hierarchical JSON structure for ...

Changing a single string column into an array of strings with a predetermined length in a pandas dataframe

I am working with a pandas dataframe that has multiple columns. My goal is to transform one of the string columns into an array of strings with a fixed length. This is how the current table is structured: +-----+--------------------+--------------------+ ...

The program is not executing properly after the initial function is invoked, causing it to deviate from its intended

Could you assist me with this source code for the hangman game? I have added an extra content genre and difficulty level to it. After playing the first round, if the player chooses to play again triggering the loop function, the words function is called o ...

Difficulty encountered while attempting to tally the amount of words within a loop

Struggling to calculate the total number of words in a for loop, encountering issues with the sum() method and unsuccessful attempts using list-append method: for line in open("jane_eyre.txt"): strip = line.rstrip() words = strip.split() for i in ...