Facing issue while providing dynamic name to file in python through a function

2024/10/13 7:22:52

the line : with open('new%s.txt' % intg ,'a') as g : is giving error in below code. Every time I call the function "Repeat", it should create file with name new1.txt, new2.txt and so on. But it is giving error : "name 'intg' is not defined" I want dynamic name for screenshot file and txt file, to prevent overwriting of file. The code is for selecting data from a software, copying it, and pasting it in new file each time. Please help:

import win32api
import win32com.client
import pyautogui
import pyperclip
shell = win32com.client.Dispatch("WScript.Shell")
win32api.Sleep(5000)def repeat( intg ):import pyautoguipyautogui.moveTo(17, 213)win32api.Sleep(2000)pyautogui.screenshot('%s.png' % intg)pyautogui.click()win32api.Sleep(2000)pyautogui.hotkey('ctrl', 'c')s = pyperclip.paste() win32api.Sleep(2000)with open('new%s.txt' % intg ,'a') as g:g.write(s)print("done")repeat( intg = 1 )
repeat( intg = 2 )win32api.Sleep(5000)
print ("done")
Answer

you can concatenate integer and strings doing so:

with open('new' + str(intg) + '.txt','a') as g:

or doing so:

with open('new{0}.txt'.format(intg),'a') as g:
https://en.xdnf.cn/q/118107.html

Related Q&A

Python Pandas: Merging data frames on multiple conditions

I wish to merge data frames as fetched via sql under multiple condition. df1: First df contains Customer ID, Cluster ID and Customer Zone ID. The second df contain complain ID, registration number.…

counterpart to PILs Image.paste in PHP

I was asked to port a Python application to PHP (and Im not very fond of PHP).The part Im having trouble to port uses a set of monochromatic "template" images based on the wonderful Map Icons…

Google Cloud Dataflow fails in combine function due to worker losing contact

My Dataflow consistently fails in my combine function with no errors reported in the logs beyond a single entry of:A work item was attempted 4 times without success. Each time the worker eventually los…

AttributeError: super object has no attribute __getattr__

Ive been searching for the solution of this problem over the all internet but I still cant find the right solution. There are lots of generic answers but none of those have solved my problem..I am tryi…

Selenium load time errors - looking for possible workaround

I am trying to data scrape from a certain website. I am using Selenium so that I can log myself in, and then start parsing through data. I have 3 main errors:Last page # not loading properly. here I am…

How to POST ndb.StructuredProperty?

Problem:I have following EndpointsModels,class Role(EndpointsModel):label = ndb.StringProperty()level = ndb.IntegerProperty()class Application(EndpointsModel):created = ndb.DateTimeProperty(auto_now_ad…

Issue computing difference between two csv files

Im trying to obtain the difference between two csv files A.csv and B.csv in order to obtain new rows added in the second file. A.csv has the following data.acct ABC 88888888 99999999 ABC-G…

How do I display an extremly long image in Tkinter? (how to get around canvas max limit)

Ive tried multiple ways of displaying large images with tkinterreally long image No matter what Ive tried, there doesnt seem to be any code that works. The main issue is that Canvas has a maximum heigh…

NoneType has no attribute IF-ELSE solution

Im parsing an HTML file and searching for status of order in it. Sometimes, status doesnt exist, so BeautifulSoup returns NoneType, when Im using it. To solve this problem I use if-else statement, but …

looking for an inverted heap in python

Id like to comb off the n largest extremes from a timeseries. heapq works perfectly for the nlargestdef nlargest(series, n):count = 0heap = []for e in series:if count < n:count+=1hp.heappush(heap, e…