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
  How to write variable in a python file then import it in another python file? tatahuft 4 994 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,114 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 1,058 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,653 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,374 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 1,620 Feb-07-2024, 12:24 PM
Last Post: Viento
  file open "file not found error" shanoger 8 8,454 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 1,964 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 2,237 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 2,137 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

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