helo! can anyone help me in automatically saving files with fixed filename in python?

the OUTPUT would be like this:

FILE- A01.txt
FILE- A02.txt
FILE- B01.txt
File- B02.txt

i was hoping that i could save a file automatically after making some changes in the older version.

Thank you,
Gene

Recommended Answers

All 6 Replies

Your description is too unclear, but generally it is not considered to change file in place as you can mess up your data.

Normal pattern is loop over the list of your files, read data preferably line by line and do changes and write them in temporary file or read whole file in memory, and do changes, preferably saving the changed data with new name or renaming the old file (add for example .bak) and using the old name for saving the data in case of in memory solution or renaming the temp file in other solution.

Sorry but I dunno how to explain it so here's my code. Im using python and I think i messed this up

def screenCapture(self, path, fileName):
		
			_path = path + '/' + self.getNewWindow()
			os.system('screencapture -x '+_path+'/'+fileName+'.png -T 0.1')
def saveScreenCapture(self, path, window, phase, img):
			
			_window = str(window)
			_phase = phase.strip('[ ]()')
			
			if len(_phase) > 3:
				_phase = _phase[0] + _phase[1] + _phase[2]
			
			fileName = 'win'+_window + '_' + _phase + '_img'+ str(img)
			
			self.screenCapture(path, fileName)
			
			return img + 1

I do not understand exactly as it is missing the context of the class definition, but I would rewrite the code you gave like:

def screencapture(self):
    os.system('screencapture -x %s.png -T 0.1' % self.filename )
    
def save_screencapture(self, path, window, phase, img):
    phase = phase.strip('[ ]()')
    phase = phase[:min(2,len(phase)]
    self.filename = os.path.join(path, 'win%s_%s_img_%s' % (window, phase, img))
    screencapture()
    return img + 1

im a little bit confused, i made a few changes in my code and this what i got. But i always get path not specified error

import os
from os.path import join
pathName = '/Users/macOSX/Desktop/screen1/'

try:
    os.makedirs(pathName)
    if pathName == 1:
        #print "True"
        path = open(pathName, "w")
        os.system('screencapture -C -tjpeg' + pathName + 'screen.jpg')
    else:
        print "Error"
except OSError:
    if os.path.exists(pathName):
        pass
    else:
        raise

what i wanted is:

1. make a new folder,
2. capture the screen
3. save the captured image (inside the created folder)

How path name can be numeric test line 7.

I got it! Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.