2

A short question. I have multiple files in a tester folder on my desktop. Now I want to rewrite all those files, add 'moved' at the end of their filename and move them to a new folder named tester1 also on my desktop. Someone any idea? Thank you in advance. This is my current code:

source = r'c:\data\AS\Desktop\tester'

#Take the absolute filepaths from all the files in tester and open them.
for file in os.listdir(source):
    file_paths = os.path.join(source, file)
    with open(file_paths, 'r') as rf:
        print(rf.read() + '\n')
ManCity10
  • 119
  • 1
  • 12

2 Answers2

0

Here is something you can use:-

import os
SourceFile="C:/Myfolder/Source/MyFile.txt"
TargetFile="C:/MyFolder/Target/MyFile_moved.txt"
os.rename(SourceFile,TargetFile)

Hope helps.

Sherin Jayanand
  • 204
  • 2
  • 9
  • I get t the following error: [WinError 2] The system cannot find the file specified: – ManCity10 May 28 '20 at 08:17
  • I have updated the code to give you the full example so that you can try it. The error you got might be because of the path or non-existence of the file (most probably the path). You could be using "\" instead of "/". Please check that once as well. – Sherin Jayanand May 28 '20 at 08:28
  • Thank you anyway, but I have used the answer above which makes it possible for multiple files. – ManCity10 May 28 '20 at 11:38
  • No issues. Happy to hear that you found a solution that worked for you. – Sherin Jayanand May 28 '20 at 12:12
0

The below code will work if you have extensions in your file and you want 'moved' to be added before extension:

    import os
    import shutil

    src_path = "c:/data/AS/Desktop/tester/"
    dest_path = "c:/data/AS/Desktop/tester1/"

    for file in os.listdir(src_path):
        file_name, extension = file.split(".")
        shutil.move(src_path + file, dest_path + file_name + "moved." + extension)

If you don't have extensions in your files, then the code can be changed as below:

    import os
    import shutil

    src_path = "c:/data/AS/Desktop/tester/"
    dest_path = "c:/data/AS/Desktop/tester1/"

    for file_name in os.listdir(src_path):
        shutil.move(src_path + file_name, dest_path + file_name + "moved")

I have checked this in MacOS, let me know in the comments if you face any issue in Windows.

Shreyansh
  • 458
  • 4
  • 11
  • I used your second block of code, it seems very logical but I get the following error: [Errno 2] No such file or directory: – ManCity10 May 28 '20 at 08:15
  • can you check now, I just fixed the issue with path. – Shreyansh May 28 '20 at 08:19
  • Now that worked, thank you very much! But what did you actually change or do to make it work? – ManCity10 May 28 '20 at 08:31
  • I replaced the backward slash to forward slash and also added one more slash in end of the path. – Shreyansh May 28 '20 at 08:37
  • Why did you chose to change those slashles? Wasnt it possible to make use of the 'r' before the pathname? – ManCity10 May 28 '20 at 09:01
  • Yes, you can use `r''` as it converts it to raw string, but I actually tested it on Mac, and in Mac the paths are of forward slash so raw string won't work. – Shreyansh May 28 '20 at 09:07