Python Forum
files creation question, any help plz ?
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
files creation question, any help plz ?
#11
You are welcome! I did it so you can see that is not so complicated when you are doing it step by step. Tiny steps. As @Kebap said. 

Look at os.path module. os.path.join() takes a bunch of arguments and join them to create a properly formated path acording to the OS. 
os.path.join(dir, subdir, filename)
import os.path

dir_ = some_dir # dir is reserved word in Python. root is more suitable variable name :-)
subdir = subdir
file_name = '{}.txt'.format(element[0])
path = os.path.join(dir_, subdir, file_name)

with open(path, 'w') as out_file:
    # etc.
You can use os.path.abspath(dir) on dir to get the full path of the dir. As a precaution.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie question about switching between files - Python/Pycharm Busby222 3 650 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Making a question answering chatbot based on the files I upload into python. Joejones 1 1,296 May-19-2023, 03:09 PM
Last Post: deanhystad
  Unable to disable creation of __pycache__ and .pyc files kpyeri 1 8,681 Oct-03-2018, 11:22 AM
Last Post: Larz60+
  Question About Files SheeppOSU 1 1,983 Jun-24-2018, 11:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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