Jump to content

[PY] Save file names with directory


Recommended Posts

  • Bot

Hello community,

I have created a python script that saves the name with directory of all files in a text file.

I used this script after organizing the quest folder from my server files to generate the quest_list.

This is the hidden content, please

Edited by Papix
  • Metin2 Dev 37
  • Eyes 1
  • Good 4
  • Love 11

english_banner.gif

Link to comment
Share on other sites

  • Honorable Member

1) You're not using if __name__ == "__main__":

2) You're not using a function to encapsulate the code #noreusability

3) You're not skipping the non .quest files

4) You're including the full path and not the relative one (C:/Users/User/Desktop/Quests/Dungeons/deviltower_zone.quest -> Dungeons/deviltower_zone.quest)

import os

def create_quest_list(directory, output_file):
    with open(output_file, 'w') as f:
        for root, _, files in os.walk(directory):
            for filename in files:
                if filename.endswith('.quest'):
                    rel_path = os.path.relpath(os.path.join(root, filename), directory)
                    rel_path = rel_path.replace(os.sep, '/')
                    f.write(rel_path + '\n')
    output_file = output_file.replace(os.sep, '/')
    print(f'File saved in {os.getcwd()}/{output_file}')

if __name__ == "__main__":
    directory = r'C:\Users\User\Desktop\Quests'
    output_file = 'quest_list.txt'
    create_quest_list(directory, output_file)

ty chatgpt

Result:

x4kmaUk.png

 

Meanwhile in freebsd:

find ~/Desktop/Quests -name "*.quest" > quest_list.txt
# without ./
find ~/Desktop/Quests -name "*.quest" -exec sh -c 'echo "{}" | sed "s|^./||"' \; > quest_list.txt

ty chatgpt^2

Edited by Metin2 Dev International
Core X - External 2 Internal
  • Metin2 Dev 1
  • kekw 1
  • Scream 1
  • Love 1
Link to comment
Share on other sites

  • Bot
2 hours ago, martysama0134 said:

1) You're not using if __name__ == "__main__":

2) You're not using a function to encapsulate the code #noreusability

3) You're not skipping the non .quest files

4) You're including the full path and not the relative one (C:/Users/User/Desktop/Quests/Dungeons/deviltower_zone.quest -> Dungeons/deviltower_zone.quest)

import os

def create_quest_list(directory, output_file):
    with open(output_file, 'w') as f:
        for root, _, files in os.walk(directory):
            for filename in files:
                if filename.endswith('.quest'):
                    rel_path = os.path.relpath(os.path.join(root, filename), directory)
                    rel_path = rel_path.replace(os.sep, '/')
                    f.write(rel_path + '\n')
    output_file = output_file.replace(os.sep, '/')
    print(f'File saved in {os.getcwd()}/{output_file}')

if __name__ == "__main__":
    directory = r'C:\Users\User\Desktop\Quests'
    output_file = 'quest_list.txt'
    create_quest_list(directory, output_file)

ty chatgpt

Result:

x4kmaUk.png

 

Meanwhile in freebsd:

find ~/Desktop/Quests -name "*.quest" > quest_list.txt
# without ./
find ~/Desktop/Quests -name "*.quest" -exec sh -c 'echo "{}" | sed "s|^./||"' \; > quest_list.txt

ty chatgpt^2

 

Thank you for the improvement of the script!

I just made a script in haste to do what I wanted to do, I didn't try to improve it.

Edited by Metin2 Dev International
Core X - External 2 Internal
  • Love 1

english_banner.gif

Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.