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
  file directory linards 3 155 Nov-07-2019, 06:18 AM
Last Post: linards
  Create a directory structure across many servers metro17 1 92 Nov-05-2019, 03:35 PM
Last Post: Larz60+
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 169 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  create a new directory if not already exists fioranosnake 1 144 Aug-28-2019, 07:26 PM
Last Post: Larz60+
  pygame.error: /etc/timidity.cfg: No such file or directory lsepolis123 3 330 Aug-23-2019, 07:53 AM
Last Post: lsepolis123
  Error -3 when opening matlab file in python python_newbie09 0 172 Aug-02-2019, 11:51 AM
Last Post: python_newbie09
  OS module and file/directory recovery hikerguy62 1 182 Aug-02-2019, 06:42 AM
Last Post: Gribouillis
  Find a given file recursively inside a directory alinaveed786 1 362 Jul-01-2019, 01:53 PM
Last Post: ichabod801
  Create file archive that contains crypto hash ED209 1 207 May-29-2019, 03:05 AM
Last Post: heiner55
  Fatal error in launcher: Unable to create process using '"' rsmldmv 0 1,530 May-13-2019, 01:34 AM
Last Post: rsmldmv

Forum Jump:


Users browsing this thread: 1 Guest(s)