开发者

argv listing directory files

开发者 https://www.devze.com 2023-03-06 14:05 出处:网络
this very simple code is driving me nuts. import sys,string,socket maquina = sys.argv[1] texto = string.join(sys.argv[2:], \" \")

this very simple code is driving me nuts.

import sys,string,socket  

maquina = sys.argv[1]
texto = string.join(sys.argv[2:], " ")
print '['+maquina+']' 
print '['+texto+']'

mysock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
if  maquina == '*':
    for ip in range(18,254):
        mysock.sendto( texto, ('192.168.0.'+str(ip),8090) )
else:
    mysock.sendto( texto, (maquina,8090))
mysock.close()

When I type

python send.py 192.168.0.135 several strings 

the output is:

[192.168.0.135]
[several strings]

But if I type

python send.py * several strings

I got:

[ajax]
[ajustaprofile.py build di开发者_开发技巧a2django.py djangoajax django-cube django-evolution django_excel_templates-0.1 Doc google_appengine httpd.conf imagens jre1.5.0_08 lib list_mailbackup.py luma models.py netsend.py pip-log.txt reverse.py share testparamet.py tnsnames.ora util virttool-0.1 xlrd-0.7.1 xlutils-1.4.1 xlwt-0.7.2 several lines]

This is the file list that I have on current directory! Python translates somehow '*' to the files of current directory.

I tried to do the homework using this link but I got no clue.

http://docs.python.org/library/sys.html http://www.faqs.org/docs/diveintopython/kgp_commandline.html

How can I use "*" on argv ?


Try using '*' instead of *. Your shell feeds results of * to the program via cmdline args, just like ls *.


Another way to get file names is using the glob module:

>>> from glob import glob
>>> glob('*')
['file1.txt', 'file2.py', 'file3', ... ]

It work just like the ls command.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号