Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mkdir() help
#1
I want to make a folder in a folder that the program is located in. The chdir() only works for computer-specific directories. any help?
Reply
#2
You can use the __file__ variable. Here is a way to create a directory named 'foo' with the standard pathlib module
from pathlib import Path

p = Path(__file__).resolve().parent/'foo'
if not p.is_dir():
    p.mkdir()
Reply
#3
(Feb-17-2018, 01:28 PM)Gribouillis Wrote: You can use the __file__ variable. Here is a way to create a directory named 'foo' with the standard pathlib module
from pathlib import Path

p = Path(__file__).resolve().parent/'foo'
if not p.is_dir():
    p.mkdir()

i used this
 def make_file():
    os.getcwd()
    os.chdir('export')
    os.mkdir(str(name.get())) 
it works as intended once now its like this
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\HOME-PC\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\HOME-PC\Desktop\program_thing\tkinter_program.py", line 20, in make_file
    os.chdir('export')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'export'
Reply
#4
Why not use my code? It is very good. In your code the os.getcwd() gets the current working directory but it does nothing with it, so you can safely remove this line. The os.chdir('export') tries to change the current directory by going to a directory named 'export', but it fails because there is no such directory. Finally the name.get() in the last statement won't work because the variable name is not defined. Or may be it is a tkinter variable in your program?
Reply
#5
(Feb-17-2018, 05:30 PM)Gribouillis Wrote: Why not use my code? It is very good. In your code the os.getcwd() gets the current working directory but it does nothing with it, so you can safely remove this line. The os.chdir('export') tries to change the current directory by going to a directory named 'export', but it fails because there is no such directory. Finally the name.get() in the last statement won't work because the variable name is not defined. Or may be it is a tkinter variable in your program?

export does exist and the name.get is part of a folder creation system
Reply
#6
Put a print statement around os.getcwd()

def make_file():
   print('Current working directory', os.getcwd())
   os.chdir('export')
   os.mkdir(str(name.get())) 
Execute the program and look in which current working directory your program is running.
Look if you can reach from this path 'export'. I think your cwd is at a different path
as the directory 'export'.

By the way, os.mkdir makes a directory, not a file.
If you want to read/write file, use open(filepath).

After you've understood the old school os.path and the difference between relative and absolute paths, you should take a look into the pathlib module.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Feb-17-2018, 06:02 PM)SteampunkMaverick12 Wrote: the name.get is part of a folder creation system
What does this mean ?
Reply
#8
(Feb-17-2018, 06:02 PM)DeaD_EyE Wrote: Put a print statement around os.getcwd()

def make_file():
   print('Current working directory', os.getcwd())
   os.chdir('export')
   os.mkdir(str(name.get())) 
Execute the program and look in which current working directory your program is running.
Look if you can reach from this path 'export'. I think your cwd is at a different path
as the directory 'export'.

By the way, os.mkdir makes a directory, not a file.
If you want to read/write file, use open(filepath).

After you've understood the old school os.path and the difference between relative and absolute paths, you should take a look into the pathlib module.

its supposed to be a folder

i am trying to make a program that makes a folder. Then you can make and save files in the folder. Its supposed to b a game modding program
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  directory not being created after os.mkdir() CAD79 3 297 Mar-16-2024, 04:00 PM
Last Post: deanhystad
  OSERROR When mkdir Oshadha 4 1,659 Jun-29-2022, 08:50 AM
Last Post: DeaD_EyE
  Python Paramiko mkdir command overwriting existing folder. How can i stop that? therenaydin 1 3,168 Aug-02-2020, 11:13 PM
Last Post: therenaydin

Forum Jump:

User Panel Messages

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