473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Attaching files in windows using Python.

Hi all,
I have to select a particular file (using the 'Browse') button in
Windows. After this I need to populate the 'Open Dialogue Box' with the
path of the file I need (I have the entier path of the file I need).
Then I need to select the 'Open' Button.

Only after this the file I want is attached.

Any idea as to how this can be done using 'Win32 API's.

While looking for the proper answer to this I found that 'Mark
Hammond's' Python for Windows documentation is not detail enough.

Any help in this regard would be much appreciated.

Mar 7 '06 #1
2 2050
Hi all,
I have got this far till now -

import win32gui, struct, array, string

OFN_ALLOWMULTISELECT=0x00000200
OFN_EXPLORER=0x00080000

def arrayToStrings(resultArray):
"""return list-of-strings corresponding to a char array,
where each string is terminated by \000, and the whole
list by two adjacent \000 bytes
"""
astr=resultArray.tostring()
manyStrings=[]
# perhaps a loop of string.split would be faster...
while len(astr) and astr[0]!='\000':
i=astr.index('\000')
manyStrings.append(astr[:i])
astr=astr[i+1:]
return manyStrings

def szFrom(anarray):
"""return the string-pointer (sz) corresponding to a char
array, 0 (null pointer) if no array
"""
if anarray: return anarray.buffer_info()[0]
else: return 0

def arrayFrom(astring,additional=0):
"""return a char array built from a string, plus 0
or more \000 bytes as filler
"""
if not astring: astring=''
return array.array('c',astring+additional*'\000')

def arrayMulti(stringlist):
"""return a char array built from many strings, each
separated by a \000 byte, and two \000's at the end
"""
return arrayFrom(string.join(stringlist,'\000'),2)

def buildOfn(resultarray,filters=None,initdir=None,tit le=None,
multisel=1,oldlook=0):
"""build an OPENFILENAME struct as a string, with several
options and a given result-array for the string[s] that
will result from the GetOpenFileName call
"""
flags=OFN_EXPLORER
if multisel: flags=flags|OFN_ALLOWMULTISELECT
if oldlook: flags=flags&~OFN_EXPLORER
szfile,maxfile=resultarray.buffer_info()
szfilter=szFrom(filters)
szinitdir=szFrom(initdir)
sztitle=szFrom(title)
return struct.pack(
"3i2P2iPiPi2PI2hPi2P",
76, 0, 0, # size, owner-hwnd, hinstance
szfilter, 0, 0, 0, # filter, custom-filter,
max-cust-filter,filter-index
szfile, maxfile, # file, max-file
0, 0, # file-title, max-file-title
szinitdir, sztitle, # initial-dir, dialog-title
flags, 0, 0, # flags, file-offset, file-extension
0, # def-ext
0, 0, 0) # cust-data, func-hook, template-name

def openNames(forsave=0,filters=None,initdir=None,titl e=None,
initfile=None,multisel=1,oldlook=0):
"""return a list of filenames for open or save, given
interactively by the user through a common-dialog; if
more than 1 string is returned, the first is the directory,
followed by the filenames.
"""
resultBuffer=arrayFrom(initfile,8192)
title=arrayFrom(title)
initdir=arrayFrom(initdir)
filters=arrayMulti(filters)
ofn=buildOfn(resultBuffer,filters,initdir,title,mu ltisel,oldlook)
if forsave: isok=win32gui.GetSaveFileName(ofn)
else: isok=win32gui.GetOpenFileName(ofn)
if not isok: return []
return arrayToStrings(resultBuffer)

def _test():
return openNames(
filters=('Texts and scripts','*.txt;*.py','Py stuff','*.py*')
)

if __name__=='__main__':
print _test()

But hear the Dialogue_box stops and waits for the user to select a
file. But Since I have the entire path of the file, How do I pass it to
the file name to populate the box automatically instead of the user
manually selecting a file.

Any further help will be appreciated.

Mar 7 '06 #2
You might want to try using win32gui.GetOpenFileNameW.
It uses keyword arguments and doesn't require that you
build a struct yourself:

win32gui.GetOpenFileNameW(File='myfile.txt', Filter='Texts and scripts\0*.txt;*.py\0Py stuff\0*.py\0')

Roger
"sri2097" <sr********@gmail.com> wrote in message news:11*********************@p10g2000cwp.googlegro ups.com...
Hi all,
I have got this far till now -

import win32gui, struct, array, string

OFN_ALLOWMULTISELECT=0x00000200
OFN_EXPLORER=0x00080000

def arrayToStrings(resultArray):
"""return list-of-strings corresponding to a char array,
where each string is terminated by \000, and the whole
list by two adjacent \000 bytes
"""
astr=resultArray.tostring()
manyStrings=[]
# perhaps a loop of string.split would be faster...
while len(astr) and astr[0]!='\000':
i=astr.index('\000')
manyStrings.append(astr[:i])
astr=astr[i+1:]
return manyStrings

def szFrom(anarray):
"""return the string-pointer (sz) corresponding to a char
array, 0 (null pointer) if no array
"""
if anarray: return anarray.buffer_info()[0]
else: return 0

def arrayFrom(astring,additional=0):
"""return a char array built from a string, plus 0
or more \000 bytes as filler
"""
if not astring: astring=''
return array.array('c',astring+additional*'\000')

def arrayMulti(stringlist):
"""return a char array built from many strings, each
separated by a \000 byte, and two \000's at the end
"""
return arrayFrom(string.join(stringlist,'\000'),2)

def buildOfn(resultarray,filters=None,initdir=None,tit le=None,
multisel=1,oldlook=0):
"""build an OPENFILENAME struct as a string, with several
options and a given result-array for the string[s] that
will result from the GetOpenFileName call
"""
flags=OFN_EXPLORER
if multisel: flags=flags|OFN_ALLOWMULTISELECT
if oldlook: flags=flags&~OFN_EXPLORER
szfile,maxfile=resultarray.buffer_info()
szfilter=szFrom(filters)
szinitdir=szFrom(initdir)
sztitle=szFrom(title)
return struct.pack(
"3i2P2iPiPi2PI2hPi2P",
76, 0, 0, # size, owner-hwnd, hinstance
szfilter, 0, 0, 0, # filter, custom-filter,
max-cust-filter,filter-index
szfile, maxfile, # file, max-file
0, 0, # file-title, max-file-title
szinitdir, sztitle, # initial-dir, dialog-title
flags, 0, 0, # flags, file-offset, file-extension
0, # def-ext
0, 0, 0) # cust-data, func-hook, template-name

def openNames(forsave=0,filters=None,initdir=None,titl e=None,
initfile=None,multisel=1,oldlook=0):
"""return a list of filenames for open or save, given
interactively by the user through a common-dialog; if
more than 1 string is returned, the first is the directory,
followed by the filenames.
"""
resultBuffer=arrayFrom(initfile,8192)
title=arrayFrom(title)
initdir=arrayFrom(initdir)
filters=arrayMulti(filters)
ofn=buildOfn(resultBuffer,filters,initdir,title,mu ltisel,oldlook)
if forsave: isok=win32gui.GetSaveFileName(ofn)
else: isok=win32gui.GetOpenFileName(ofn)
if not isok: return []
return arrayToStrings(resultBuffer)

def _test():
return openNames(
filters=('Texts and scripts','*.txt;*.py','Py stuff','*.py*')
)

if __name__=='__main__':
print _test()

But hear the Dialogue_box stops and waits for the user to select a
file. But Since I have the entire path of the file, How do I pass it to
the file name to populate the box automatically instead of the user
manually selecting a file.

Any further help will be appreciated.


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ravi | last post by:
Q1.)In SQL Server 2000, is it always possible to use copies of the data and transaction log files of a database from one server to reattach to a new database on another server, or even to the same...
0
by: Roshan James | last post by:
Hi, I'm creating an application which requires to store multiple file attachments in SQL Server Database from a windows form. I am able to store the files and retreive the files from the...
3
by: Brian Farnhill (MCP VB.NET) | last post by:
Hi, I am having some trouble using the MailMessage object to send an email with more than one attachment. I am working on a web based application where a user can submit information, along...
2
by: john.lehmann | last post by:
Attacked is a piece of code which first hits the login page successfully and receives back login cookies. But then when I attempt to hit a page which is restricted to logged in users only, I fail....
4
by: Greg P | last post by:
I know this is a long post, please bear with me. I have been working on this all weekend to no avail although I have done a good amount of research (see most pertinent links that I've looked at...
0
by: wal | last post by:
How does one attach files to emails using libgmail? The following code http://pramode.net/articles/lfy/fuse/4.txt works fine when said files are simple text files, but it failes as soon as the...
5
by: Robert Dufour | last post by:
I am trying to use framework 1.1 - stuck with it. to send emails from a windows form application. The email messages can have attachments, usually two and they can be either text or sounds (wav...
2
by: ARC | last post by:
(sorry for the re-post, but I thought I would put the winmail.dat where it's visible in the subject...thanks!) Hello all, I have a question that I hope someone can help with. I have an Access...
10
by: sandipm | last post by:
Hi, In my application, I have some configurable information which is used by different processes. currently I have stored configration in a conf.py file as name=value pairs, and I am importing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.