0

I am trying to get the string dynamically and use regex over it but i get the error:

coding partial snippet:

import re
def file_save():
    f = th1.asksaveasfile(mode='w', defaultextension=".txt")
    re.search(r"<[^>]*\bname='([^']*)", f).group(1)       
    f.close()

Error

    re.search(r"<[^>]*\bname='([^']*)", f).group(1)
  File "C:\Python34\lib\re.py", line 166, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

Here,the input string has to be attained from a 'filename to be saved' which produces the text 'f' as:

<_io.TextIOWrapper name='C:/Python34/abcd.txt' mode='w' encoding='cp1252'>

the regex works perfect,but icannot get the string and get the ouput as:

C:/Python34/abcd.txt

Please help me to fix my problem.Answers will be appreciated!

1 Answer 1

1

I think you're using wrong tool.

If you want to get filename, just use asksaveasfilename instead of asksaveasfile.

def file_save():
    filename = th1.asksaveasfilename(defaultextension=".txt")
    # do something with the filename

Or, if you need to operate on the file, use asksaveasfile and use name attribute of the file returned.

def file_save():
    f = th1.asksaveasfile(mode='w', defaultextension=".txt")
    filename = f.name
    # do something with the filename
    f.close()
Sign up to request clarification or add additional context in comments.

4 Comments

but,how do i get the input for the regex from filename saved?
@user3914506, What do you mean? You don't need to use regular expression to get filename.
then how could I get my Filename stored and save in a variable say x?
@user3914506, I updated the answer. Please check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.