WowGodz -
WowGodz -

Reputation: 38

Replace a character in a text file python

import os
from pynput.mouse import Button, Controller
from win32api import GetSystemMetrics

A = 1
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
middle = ((width / 2), (height / 2))
TL1 = ((width / 2) / 2, (height / 2) / 2)
TR2 = (width / 2) * 1.5, (height / 2) / 2
BL3 = ((width / 2) / 2), ((height / 2) * 2) / 1.5
BR4 = (width / 2) * 1.5, ((height / 2) * 2) / 1.5

mouse = Controller()
mousePos = mouse.position
run = True
while run:
    # file1 = open("fp.txt")

    mousePos = mouse.position
    if mousePos == mouse.position:
        mouse.position = mousePos
        # mouse.position = file1.read().splitlines()
        with open('fp.txt') as f:
            f.replace('(', '')
            [tuple(map(float, i.split(','))) for i in f]

Text File:

(304, 898)
(304, 898)
(304, 898)
(304, 898)
(304, 898)
(305, 998)

ERROR

    f.replace('(', '')
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

im trying to get it as a int without /n. I would get for example '(306 , 465) \n' , then it was (306 because I wanted to separate it but now im getting the above error. I trying to get it in '(300,343)' as a tuple so I could use it in 'mouse.position'. For example mouse.position = (1,1). The code I used to get the points is

import os
from pynput.mouse import Button, Controller
from win32api import GetSystemMetrics

A = 1
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
middle = ((width / 2), (height / 2))
TL1 = ((width / 2) / 2, (height / 2) / 2)
TR2 = (width / 2) * 1.5, (height / 2) / 2
BL3 = ((width / 2) / 2), ((height / 2) * 2) / 1.5
BR4 = (width / 2) * 1.5, ((height / 2) * 2) / 1.5

mouse = Controller()
mousePos = mouse.position
run = False
if not run:
    run2 = True
run2 = True
while run2:
    a = input("Restart(Y/N):")
    a = a.lower()
    if a == "y" or "yes":
        run = True
        run2 = False
        os.remove("fp.txt")
while run:
    if mousePos == mouse.position:
        A += 1


    else:
        for i in range(1):
            for j in range(2):
                print(mouse.position)

    mouse.position
    mousePos = mouse.position
    if mousePos == mouse.position:
        mouse.position = mousePos

    file1 = open("fp.txt", "a+")
    file1.writelines(str(mousePos) + "\n")

but this works fine its just the other one.

edit

I want my mouse to mimic itself using those points

Upvotes: 1

Views: 333

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51653

You cannot use str.replace() on a file object - you need a string for that.

This reads all points from your file once and stores it in a set (if order/duplicates matters use list instead) for later usage:

points = set()
with open ("mousepoints.txt") as f:
    # each line seperately
    for idx,line in enumerate(f):
        # strip \n  & strip ( and ) & split at ,
        pointstring = line.strip().strip("()").split(",")
        # guard against empty/non well formed lines
        if pointstring and len(pointstring) == 2 :
             points.add(tuple(map(float,pointstring)))
        else:
             print(f"Error in line {idx}: '{line}'")

print(points)

Your code has other things in it that make no sense:

mousePos = mouse.position
if mousePos == mouse.position:

This condition is (for all what mattes) always true - you set the mousePos one line earlier to be exactly this. I highly doubt that you will be able to move and process the mouse exactly between this two lines.

  [tuple(map(float, i.split(','))) for i in f]

Creates a list that is immideately forgotten - why do that?

while run2:
    a = input("Restart(Y/N):")
    a = a.lower()
    if a == "y" or "yes":

The condition if a == "y" or "yes": is ALWAYS true - this tests if a == "y" or "yes" == true and non-emtpy strings are truthy - correctly you can do this like so:

if a in ("y","yes"):
if a == "y" or a == "yes":

etc.

Upvotes: 3

Related Questions