data123
data123

Reputation: 19

How to increment a variable (with number) inside python script?

Using Below given python script, I am trying to copy a text file and trying to create 10 copies of the same file as shown below:

logs1.txt
logs2.txt
.
.
logs10.txt

Basically i need to increment only the number present in destination text file name.

I have tried using "for loop" with range command. But I dont know how to increment a number inside a cp command.

#/usr/bin/env python
import subprocess

class RunCmd(object):
    def cmd_run(self, cmd):
        self.cmd = cmd
        subprocess.call(self.cmd, shell=True)


a = RunCmd()
a.cmd_run('cp /sh/awlog/Logs_new_1.txt /sh/logs/logs1.txt')

Upvotes: 0

Views: 2037

Answers (5)

RoadRunner
RoadRunner

Reputation: 26335

The other answers are good, but you can also do this the old fashioned way:

# Open main file
with open("/sh/awlog/Logs_new_1.txt") as f_in:

    # Read in text
    file_str = f_in.read()

    # Iterate 1 - 10
    for i in range(1, 11):

        # Open new file to write to with number
        with open("/sh/logs/logs%d.txt" % i, mode="w") as f_out:
            f_out.write(file_str)

You can simply iterate over range(1, 11) to get the numbers 1 - 10, excluding 11. You can have a look at range() for behavior specifics.

Addtionally, as pointed out in the other answers, the good thing about shutil.copyfile() is that it avoids the need to open files, as shown above, and you can copy the content over directly with no issues.

Having said this, the source code does still open files under the hood, so I guess that's something to consider.

Upvotes: 0

najeem
najeem

Reputation: 1921

The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.

import shutil

for i in range(10):
    shutil.copyfile(
        "/sh/awlog/Logs_new_1.txt",
        "/sh/logs/logs{}.txt".format(i+1)
    )

Upvotes: 4

user10859576
user10859576

Reputation:

It should be as simple as

import shutil

for i in range(10):
    shutil.copyfile('/path/to/source/file','/destination/path/logs'+str(i+1)+'.txt')

There's no need to create a class to carry out such simple functionality. Also, copying files can be done using the shutil module directly rather than creating a subprocess to do so.

More information about shutil: How do I copy a file in Python?

Edit:

alternate code as per Bruno's suggestions:

import shutil

for i in range(1,11):
    shutil.copyfile('/path/to/source/file','/destination/path/logs{}.txt'.format(i))

Upvotes: 4

buran
buran

Reputation: 14273

You should use string formatting

templ = 'cp /sh/ec_rawlog/Logs_new_{0}.txt /sh/logs/logs{0}.txt'
for num in range(1,11):
    print(templ.format(num))

output

cp /sh/ec_rawlog/Logs_new_1.txt /sh/logs/logs1.txt
cp /sh/ec_rawlog/Logs_new_2.txt /sh/logs/logs2.txt
cp /sh/ec_rawlog/Logs_new_3.txt /sh/logs/logs3.txt
cp /sh/ec_rawlog/Logs_new_4.txt /sh/logs/logs4.txt
cp /sh/ec_rawlog/Logs_new_5.txt /sh/logs/logs5.txt
cp /sh/ec_rawlog/Logs_new_6.txt /sh/logs/logs6.txt
cp /sh/ec_rawlog/Logs_new_7.txt /sh/logs/logs7.txt
cp /sh/ec_rawlog/Logs_new_8.txt /sh/logs/logs8.txt
cp /sh/ec_rawlog/Logs_new_9.txt /sh/logs/logs9.txt
cp /sh/ec_rawlog/Logs_new_10.txt /sh/logs/logs10.txt

Upvotes: 1

Wouter Goossens
Wouter Goossens

Reputation: 121

Since the a.cmd_run command-argument contains a string, you can just change this string in a while-loop.

i = 1
while i < 11:
    string = 'cp /sh/ec_rawlog/Logs_new_1.txt /sh/logs/logs%s.txt' % i
    a = RunCmd()
    a.cmd_run(string)
    i += 1

Upvotes: 2

Related Questions