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
  Getting FileNotFoundError: [Errno 2] ,if tries to acees same file from sub-directory pmpinaki 2 324 May-04-2020, 03:39 PM
Last Post: pmpinaki
  Pyinstaller create this error :“File ”multiprocessing\connection.py“, line 691 Formationgrowthhacking 2 245 Apr-30-2020, 10:26 AM
Last Post: buran
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 155 Apr-29-2020, 07:10 PM
Last Post: deanhystad
  How to create a Log file skaailet 0 165 Mar-24-2020, 12:20 AM
Last Post: skaailet
  Recommended way to read/create PDF file? Winfried 1 295 Mar-17-2020, 03:17 PM
Last Post: Larz60+
  FileNotFoundError: [Errno 2] No such file or directory saqib1066 23 2,907 Mar-16-2020, 01:25 PM
Last Post: saqib1066
  Error With Reading Files In Directory And Calculating Values chascp 2 240 Feb-15-2020, 01:57 PM
Last Post: chascp
  Python 2.7 Import error. Directory and filename conflict petcoo00 2 272 Feb-02-2020, 08:46 PM
Last Post: snippsat
  Problem after changing Python directory Souila7 1 232 Jan-25-2020, 05:06 PM
Last Post: ThiefOfTime
  Create a csv file that pulls data from an .accdb file T0ky0banana 0 292 Dec-20-2019, 01:32 AM
Last Post: T0ky0banana

Forum Jump:


Users browsing this thread: 1 Guest(s)