1

After renaming a file unknown.txt to New_title.txt, how is it possible to save a file on above it of it so that it saves a file inside the folder?

"names.txt" inside file text

New_title.txt

"FolderOutput" inside folder

unknown.txt

after renaming

FolderOutput/New_title.txt

code

import glob
import os

path_to_images = glob.glob(r"FolderOutput/unknown.txt")

with open('names.txt') as f:
    for line, file in zip(f, path_to_images):
        os.rename(file, line.strip())

Code works but does not save a file in the same folder "FolderOutput"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Why are you using a glob on a single file? What do you expect the zip function to return? And I don't think you can rename a file and move to a folder in the same operation – OneCricketeer Jun 04 '23 at 13:36
  • @OneCricketeer Because first I need to save one file in the same folder "FolderOutput". Then I will make a loop using glob myself for my project – komiratna521 Jun 04 '23 at 13:42
  • Does this answer your question? [How do I move a file in Python?](https://stackoverflow.com/questions/8858008/how-do-i-move-a-file-in-python) – STerliakov Jun 04 '23 at 13:47
  • You can `os.chdir` to desired directory, or prepend directory to desired path (ideally with `pathlib` module or `os.path.join` if you resist from class-based approach) on every iteration - Something like `source = Path("FolderOutput/"); with open('names.txt') as f: for line, src in zip(f, path_to_images.glob('unknown.txt')): src.rename(source / line.strip())` – STerliakov Jun 04 '23 at 13:50
  • Also beware that filesystem iteration order is not fixed, so with several files your code will rename some input files to some output files, but in random order (both `a -> new_a, b -> new_b` and `a -> new_b, b -> new_a` can happen, if your input file is `new_a\nnew_b` and folder contains `a` and `b`). – STerliakov Jun 04 '23 at 13:55
  • @SUTerliakov I will have to make two code. The first is to rename a single file. The second is to move a file with a specific extension to a folder "FolderOutput" – komiratna521 Jun 04 '23 at 14:00
  • @SUTerliakov After trying, I got the following error: NameError: name 'Path' is not defined . Even though I added a shortpath or full path, the same error – komiratna521 Jun 04 '23 at 14:17
  • `from pathlib import Path`, of course. – STerliakov Jun 04 '23 at 15:00
  • @SUTerliakov I tried it before. I just got another error: NameError: name 'path_to_images' is not defined https://controlc.com/95b40943 – komiratna521 Jun 04 '23 at 15:17
  • Sorry, hard to detect all typos in comment, it should be `source.glob`, but I think this should be obvious to spot... – STerliakov Jun 04 '23 at 16:05

1 Answers1

0

[I hope you have these files like 'names.txt' in the same folder as the python script]

I am pretty new to python, so i dont know how os or glob work, but couldn't you just create a new file with your chosen name and drop the previous file for the same effect? i think you already know how that can be done, but if you still need the code, here it is:

import os

f = open('names.txt', 'r')
name = f.read().strip("\n")
f.close()
f = open("path/unknown.txt", 'r')
text = f.read()
f.close()

os.remove('unknown.txt')

f = open(f"path/{name}", "w")
f.write(text)
f.close()

DhruvFlare
  • 31
  • 4
  • Thank you for the answer, but the code does not do anything, it just deletes the unknown.txt file. Even after deleting the os.remove line nothing works – komiratna521 Jun 04 '23 at 14:35
  • After adding a path "os.remove('path/unknown.txt')" , Works now, thanks for the help – komiratna521 Jun 04 '23 at 15:30
  • i said path as in the path to the txt file... so as long as you can write a relative or exact path, it should work ... also, this is probably not the best way to do this..but its presently the only way i know how to. – DhruvFlare Jun 14 '23 at 16:04