2013-11-20 84 views
0

我有一个像分割使用分隔符“/”在Linux

/test1/test2/test3/test4/test5/foo/something_else.csv, 2013-11-25, username, group 
/test1/test2/test3/test4/test5/split/public/something_else.csv, 2013-11-27, username, group 
/test1/test2/test3/test4/test5/dashboard/private/tags/test.csv, 2013-11-25, user, no_group 

我需要在下面的格式输出

输出文件的文件:

/test1/test2/test3/test4/test5/foo, 2013-11-25, username, group 
/test1/test2/test3/test4/test5/split, 2013-11-27, username, group 
/test1/test2/test3/test4/test5/dashboard, 2013-11-25, user, no_group 

任何人都可以请提供给我该场景的解决方案... 谢谢

+1

当你从sys import argv使用时,你得到了什么? – Random832

回答

1

没有argv模块。 argvsys模块的属性:

import sys 
script, first, second, third = sys.argv 

或使用:

from sys import argv 
script, first, second, third = argv 

这是假设你恰好路过3命令行参数到脚本。

+0

另请注意,如果脚本没有完全3个参数,则脚本将不会运行。 – Nico

+0

@SethMMorton:哎呀,当然。 –

+0

@尼科当然,但这个评论可能属于原本的问题本身,而不是这个答案。 – SethMMorton

1

问题是argv不是python包。声明import sys用于导入sys程序包,argv是此程序包的一个变量,用于保存程序的参数。 进口SYS后:import sys可以使用的argv这样的:

script, first, second, third = sys.argv 
print "the script is called:", script 
print "First script:", first 
print "Second script:", second 
print "third script:", third 

或替代你可以使用from sys import argv像hcwhsa指出并使用它,你张贴的方式相同。

+0

知道了...谢谢 – disizjay