推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
zyqf
V2EX  ›  Python

请把下列代码改成一行代码,请让我见证奇迹,好吗?

  •  
  •   zyqf · Aug 4, 2015 · 5057 views
    This topic created in 4108 days ago, the information mentioned may be changed or developed.

    from sys import argv
    from os.path import exists

    script, from_file, to_file = argv

    print "Copying from %s to %s" % (from_file, to_file)

    we could do these two on one line too, how?

    input = open(from_file)
    indata = input.read()

    print "The input file is %d bytes long" % len(indata)

    print "Does the output file exist? %r" % exists(to_file)
    print "Ready, hit RETURN to continue, CTRL-C to abort."
    raw_input()

    output = open(to_file, 'w')
    output.write(indata)

    print "Alright, all done."

    output.close()
    input.close()

    作者说,他能用一行代码写出来。。。。

    Supplement 1  ·  Aug 4, 2015
    只能这样写了,实在没办法做到一行代码
    from sys import argv
    open(argv[2], 'w').write(open(argv[1]).read())
    21 replies  •  2016-04-23 04:17:46 +08:00
    zyqf
    zyqf
    OP
    Aug 4, 2015
    2
    @hahastudio
    from sys import argv
    script, from_file, to_file = argv

    这2行代码是必须的吧?
    ytf4425
    ytf4425
    Aug 4, 2015
    3
    外行人来欣赏艺术来了
    看来码农也有码神啊
    zyqf
    zyqf
    OP
    Aug 4, 2015
    4
    @ytf4425 新手,正在学习python,实在是百度不到答案,迫不得已来v2ex求解
    hahastudio
    hahastudio
    Aug 4, 2015
    5
    @zyqf 不嫌难看的话,你总可以用 ; 分隔
    功能上讲的话,那些答案都说明了
    expkzb
    expkzb
    Aug 4, 2015
    6
    we could do these two on one line too, how?
    意思不是把特指的两行变一行么
    minvacai
    minvacai
    Aug 4, 2015
    7
    cp from_file to_file #你够
    zyqf
    zyqf
    OP
    Aug 4, 2015
    8
    @minvacai 如果我没记错的话,这是linux命令吧。。。。
    zyqf
    zyqf
    OP
    Aug 4, 2015
    9
    @expkzb 书上的加分练习时说,把上面的代码改写成1行。 那个注释,不知道为什么#号没了,是另一个小练习
    zyqf
    zyqf
    OP
    Aug 4, 2015
    10
    @hahastudio 我还以为有什么复杂的写法呢。。。。
    qw7692336
    qw7692336
    Aug 4, 2015
    11
    bububut
    bububut
    Aug 4, 2015
    12
    楼主需提高阅读理解水平,没让你把所有代码变成一行
    leavic
    leavic
    Aug 4, 2015
    13
    lambda(语句1 and 语句2 and 语句3)
    minvacai
    minvacai
    Aug 4, 2015
    14
    @zyqf 果然当时应该写“(逃”
    zyqf
    zyqf
    OP
    Aug 4, 2015
    15
    @bububut 看看你能把这个脚本改多短,我可以把它写成一行。

    原话是这样的。。。。。
    gamexg
    gamexg
    Aug 4, 2015❤️1
    16
    指的是这个?还是全部?

    print 'Copying from %s to %s'%(__import__('sys').argv[1:])
    zyqf
    zyqf
    OP
    Aug 4, 2015
    17
    @gamexg

    from sys import argv
    open(argv[2], 'w').write(open(argv[1]).read())
    zhuangzhuang1988
    zhuangzhuang1988
    Aug 6, 2015
    18
    再怎么复杂的代码都能 exec "from sys import argv;open(argv[2], 'w').write(open(argv[1]).read())"
    WKPlus
    WKPlus
    Aug 6, 2015
    19
    最后的写法,文件都没关闭。干嘛非要一行写呢,又不是越少越优雅
    saber000
    saber000
    Aug 6, 2015
    20
    open(__import__("sys").argv[2], "wb").writelines(open(__import__("sys").argv[1]))
    goldalan
    goldalan
    Apr 23, 2016
    21
    from sys import argv;open(argv[2], 'w').write(open(argv[1]).read());print("Great")
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4987 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 07:23 · PVG 15:23 · LAX 23:23 · JFK 02:23
    ♥ Do have faith in what you're doing.