简体   繁体   中英

How do i ignore the space in command line input in python

So i need to append to a file in python. This is a random command line input

Example 1:

file1 This is a sentence.

How do i make it so it "This is a sentence" would be appended to file1 not just "This"

Example 2:

file2 A sentence

How do i make it so it "A sentence" would be appended to file2 not just "A"

This is what i have so far. And any random sentence could be appended to the files

with open(input[1],'a') as fileA:
        fileA.write(input[2])

Pass the second command line arguments as a single argument in the shell:

python /path/to/script.py file1 "This is a sentence."

Alternatively, join the arguments after the filename using str.join :

with open(input[1], 'a') as fileA:
    fileA.write(' '.join(input[2:]))

But the alternative solution will not preserve exact count of spaces / tabs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM