Python Forum
Copy a File & Increment File Count
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy a File & Increment File Count
#1
Hi guys,

I try to create a script with the following code but getting errors. Can some one help? Why the main() can't be used with the import?

Basically the script is to create a copy of file from "581_nestedclipsdrag.swf" with a new file name with the count of 10 files, e.g. file-1 to file-10.

root@lab:/tmp/create# python ./testDup.py
File "./testDup.py", line 5
import shutil

^
IndentationError: expected an indented block


#!/usr/bin/env python

def main():
import shutil
for i in range(10):
newPath = shutil.copy('581_nestedclipsdrag.swf', 'file-%s.swf' % i)

if __name__== "__main__":
main()
Reply
#2
You should put code into bb-code-tags. Otherwise white space / indentation is lost.


Fix your indentation. Don't use tabulator, use white space.
Put import statements on the top of the file.
Don't use import statements in functions or classes.
Use new-style formatting with the format method.
Use Arguments in your function.
Use Python 3.

#!/usr/bin/env python3

"""
Description what the code does
"""
import shutil


def main(source):
    fmt = 'file-{}.swf'
    for i in range(10):
        target = fmt.format(i)
        shutil.copy(source, target)


if __name__== "__main__":
    main('581_nestedclipsdrag.swf')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 284 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 453 Feb-07-2024, 12:24 PM
Last Post: Viento
  file open "file not found error" shanoger 8 1,153 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 775 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 938 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,114 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,132 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  is it possible to copy image from email and place into excel file? cubangt 3 1,282 Nov-30-2022, 05:11 PM
Last Post: snippsat
  help to increment a third list hermine 7 1,351 Nov-29-2022, 04:19 PM
Last Post: perfringo
  Please help me [copy and paste file from src to dst] midomarc 2 1,026 Nov-24-2022, 10:13 PM
Last Post: midomarc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020