Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python Directory and File Create Error
#1
I am trying to create a Directory (or say, Folder) under the "/tmp/" path.

There, I want to create a Writable Binary file and dump some data. I was doing that in the following way, but neither the Directory nor the File is getting created.
...
fnamehead,fnametail=os.path.split(name) #'name' contains a arbitrary filepath
fname='/tmp/'+fnametail
os.makedirs(os.path.dirname(fname))

with open ('xyz','ab') as fd:
	fd.write(data)
...
Please help me with that.
Quote
#2
From your code os.path.dirname(fname) will always return '/tmp'. So effectively you always try to create '/tmp' if it does not exists.

import os

fnametail = 'somefolder'
fname='/tmp/'+fnametail
print(os.path.dirname(fname))
Output:
/tmp
And because you don't specify path, 'xyz' file is created in current working directory (i.e. the one from which you run your code)

As a side note - use os.path.join() to construct paths, not concatenation
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 108 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  create a new directory if not already exists fioranosnake 1 101 Aug-28-2019, 07:26 PM
Last Post: Larz60+
  pygame.error: /etc/timidity.cfg: No such file or directory lsepolis123 3 210 Aug-23-2019, 07:53 AM
Last Post: lsepolis123
  Error -3 when opening matlab file in python python_newbie09 0 117 Aug-02-2019, 11:51 AM
Last Post: python_newbie09
  OS module and file/directory recovery hikerguy62 1 138 Aug-02-2019, 06:42 AM
Last Post: Gribouillis
  Find a given file recursively inside a directory alinaveed786 1 288 Jul-01-2019, 01:53 PM
Last Post: ichabod801
  Create file archive that contains crypto hash ED209 1 170 May-29-2019, 03:05 AM
Last Post: heiner55
  Fatal error in launcher: Unable to create process using '"' rsmldmv 0 910 May-13-2019, 01:34 AM
Last Post: rsmldmv
  charmap codec can't decode byte error with gzipped file in python bluethundr 2 326 Apr-30-2019, 12:26 PM
Last Post: bluethundr
  How to fix error code 2 in python, “directory not found”? dav3javu 1 369 Apr-03-2019, 04:55 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)