#๐Ÿ”’ someone explain this

18 messages ยท Page 1 of 1 (latest)

frigid bloomBOT
#

@trail musk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

coral crater
#

you probably need to add the folder name in the file path to it while renaming

trail musk
#

how would it know what the file is called

#

he said i cant find the file called this

#

how would he know its name if he cant find it

#

if the only way to get the name is through iterating

#

he obviously can find it if he can name it

coral crater
#

the os.listdir is one process. os.rename is a separate process

#

!d os.rename

frigid bloomBOT
#

os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)```
Rename the file or directory *src* to *dst*. If *dst* exists, the operation will fail with an [`OSError`](https://docs.python.org/3/library/exceptions.html#OSError) subclass in a number of cases:

On Windows, if *dst* exists a [`FileExistsError`](https://docs.python.org/3/library/exceptions.html#FileExistsError) is always raised. The operation may fail if *src* and *dst* are on different filesystems. Use [`shutil.move()`](https://docs.python.org/3/library/shutil.html#shutil.move) to support moves to a different filesystem.
trail musk
#

oooooooooo

coral crater
#

So what os.listdir is able to find is not shared with os.rename

#

provide the full file path in os.rename

trail musk
#

you genius

fallow aspen
#
from pathlib import Path

for file_path in Path("spriteicons").iterdir():
    new_file_path = file_path.with_name("civ_" + file_path.name[4:].lower())
    file_path.rename(new_file_path)
frigid bloomBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.