basically i am trying to read and edit the .cmd and .tcl file through python and edit it or place some string words and overwrite the existing code ...pls help urgently i am already delayed in submitting my project.....
import re
SUB_TABLE = {
'Virtex4': 'Virtex5',
}
FILENAME = 'input.tcl'
REGEX_TEMLATE = r'project set\s+("?[^"]*\S"?)\s+"{0}"'
REPLACE_TEMPLATE = r'project set \1 "{0}"'
if __name__ == '__main__':
with open(FILENAME) as fh:
content = fh.read()
for from_val, to_val in SUB_TABLE.items():
regex = REGEX_TEMLATE.format(from_val)
replace_string = REPLACE_TEMPLATE.format(to_val)
content = re.sub(regex, replace_string, content)
with open(FILENAME, 'w') as wfh:
wfh.write(content)
[color=#40BFBF]
from Tkinter import *
import os
import tkMessageBox
import shutil
import re
global newpath
prr=Tk()
prr.title("RCS TOOL")
prr.geometry('700x700+700+700')
var = StringVar()
var.set("Project Name")
label=Label( prr,textvariable=var,height=3)
label.config(font=('arial',11))
label.place(x=100,y=10)
name_entry=Entry(prr)
name_entry.place(x=200,y=30)
def new_proj():
newpath = r"C:\Documents and Settings\KSR\Desktop\%s"%name_entry.get()
if not os.path.exists(newpath): os.makedirs(newpath)
shutil.copy2('C:\Documents and Settings\KSR\Desktop\myproj\soth.tcl',newpath)
newpath=newpath+"\soth.tcl"
pat = re.compile('virtex4')
var1=StringVar()
def new_value(mat,dic = {'virtex4':'virtex5'}):
return dic[mat.group()]
with open(r'newpath','rb+') as f:
content = f.read()
f.seek(0,0)
f.write(pat.sub(new_value,content))
f.truncate()
b1= Button(prr,newpath,text="create new project",padx=10,pady=3,command=new_proj)
b1.place(x=330,y=25)
prr.mainloop() [/color]
newpath=newpath+"\soth.tcl"
with open(r'newpath','rb+') as f:
vishwas wrote:move the original declaration of newpath from the global scope....means ????......
vishwas wrote:
- Code: Select all
with open(r'newpath','rb+') as f:
do you think this syntax is correct to read the file with new path ....
import os
import re
import shutil
from Tkinter import *
SUB_TABLE = {
'Virtex4': 'Virtex5',
}
FILEPATH = 'input.tcl'
NEWPATH = 'new_location'
REGEX_TEMLATE = r'project set\s+("?[^"]*\S"?)\s+"{0}"'
REPLACE_TEMPLATE = r'project set \1 "{0}"'
def new_proj():
if not os.path.exists(NEWPATH):
os.makedirs(NEWPATH)
shutil.copy2(FILEPATH, NEWPATH)
new_filepath = os.path.join(NEWPATH, os.path.basename(FILEPATH))
with open(new_filepath) as fh:
content = fh.read()
for from_val, to_val in SUB_TABLE.items():
regex = REGEX_TEMLATE.format(from_val)
replace_string = REPLACE_TEMPLATE.format(to_val)
content = re.sub(regex, replace_string, content)
with open(new_filepath, 'w') as wfh:
wfh.write(content)
if __name__ == '__main__':
prr=Tk()
prr.title("RCS TOOL")
prr.geometry('700x700+700+700')
var = StringVar()
var.set("Project Name")
label=Label( prr,textvariable=var,height=3)
label.config(font=('arial',11))
label.place(x=100,y=10)
name_entry=Entry(prr)
name_entry.place(x=200,y=30)
b1= Button(prr,text="create new project",padx=10,pady=3,command=new_proj)
b1.place(x=330,y=25)
prr.mainloop()
vishwas wrote:Thank you very much for the help....but sir i am kind a novice to the python programming...so i am not able to understand it fully...![]()
...
vishwas wrote:now in your code.....regex_temlate and replace_template ????...
vishwas wrote: if you can give me the code which i can directly copy and paste it than it will be vollaaaa.....![]()
import os
import re
import shutil
from Tkinter import *
SUB_TABLE = {
'Virtex4': 'Virtex5', #------->and here also i want user input from the drop box menu..... :roll:
}
FILEPATH =(r'C:\Users\vishwas\Desktop\soth.tcl')
NEWPATH =(r"C:\Users\vishwas\Desktop\%s")%name_entry.get() #---------->>>>>at this point it saying name_entry.get() is not
defined bcoz i am declaring entry widget
later...how to resolve this now :x
REGEX_TEMLATE = r'project set\s+("?[^"]*\S"?)\s+"{0}"'
REPLACE_TEMPLATE = r'project set \1 "{0}"'
def new_proj():
if not os.path.exists(NEWPATH):
os.makedirs(NEWPATH)
shutil.copy2(FILEPATH, NEWPATH)
new_filepath = os.path.join(NEWPATH, os.path.basename(FILEPATH))
with open(new_filepath) as fh:
content = fh.read()
for from_val, to_val in SUB_TABLE.items():
regex = REGEX_TEMLATE.format(from_val)
replace_string = REPLACE_TEMPLATE.format(to_val)
content = re.sub(regex, replace_string, content)
with open(new_filepath, 'w') as wfh:
wfh.write(content)
if __name__ == '__main__':
prr=Tk()
prr.title("RCS TOOL")
prr.geometry('700x700+700+700')
var = StringVar()
var.set("Project Name")
label=Label( prr,textvariable=var,height=3)
label.config(font=('arial',11))
label.place(x=100,y=10)
name_entry=Entry(prr)
name_entry.place(x=200,y=30)
b1= Button(prr,text="create new project",padx=10,pady=3,command=new_proj)
b1.place(x=330,y=25)
prr.mainloop()
Users browsing this forum: No registered users and 1 guest