try except is not working while using hdfs command

0 votes

Hi,
 I am trying to run following things in code, but it's not working
def create_if_not_exists(dir_fullpath):
  """Creates a given directory if it doesn't exist yet

 Args:                                                                                                                                                                                                     
    dir_fullpath: full path to the directory                                       
  Inspired by: http://stackoverflow.com/a/600612 

  if dir_fullpath.startswith("/hdfs") :
    print 'Entering hdfs area'
    hdfsdir = re.sub('/hdfs','',dir_fullpath)
    cmd  = 'hdfs dfs -mkdir -p %s' %hdfsdir
    try:
      os.system(cmd)
      print ' directory is created successfully'
    except OSError as exc:
      print 'error'
      print 'directory is not created'
      cmd = 'hdfs dfs -test -d %s' %hdfsdir
      direxist = os.system(cmd)==0
      if exc.errno == errno.EEXIST and direxist :
        pass
      else:
        raise
  else :
    print 'Entering home dir'
    cmd = 'mkdir %s' %dir_fullpath
    try :
      os.system(cmd)
      #os.makedirs(dir_fullpath)                                                                                                                                                                            
      print ' directory is created succesfully'
    except OSError as exc:
      print 'directory is not created'
      if exc.errno == errno.EEXIST and os.path.isdir(dir_fullpath):
        pass

      else:

        return

while it works when directory name starts w/ 'home ', but it gets stuck, try:
      os.system(cmd)
      print ' directory is created successfully' when directory name starts w/ '/hdfs'. Could you please help me to solve this problem?
Thanks

Mar 6, 2019 in Python by anonymous
1,116 views
Seems like some problem with the hdfs path. Could you please the post the path that you are using?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

Raw_input method is not working in python3. How to use it?

raw_input is not supported anymore in python3. ...READ MORE

answered May 5, 2018 in Python by aayushi
• 750 points
3,301 views
0 votes
3 answers

'python' is not recognized as an internal or external command

Try "py" instead of "python" from command line: C:\Users\Cpsa>py Python 3.4.1 (v3.4.1:c0e311e010fc, May ...READ MORE

answered Feb 8, 2022 in Python by Rahul
• 9,680 points
2,454 views
0 votes
1 answer

Global Send Keys - Input text which has '(' is not working

Global Send Keys internally uses System.Windows.Forms.SendKeys so special characters escaped. For more ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,260 points
2,468 views
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26, 2019 in Python by Neel
• 3,020 points
985 views
0 votes
0 answers

Why is selenium css and xpath selector not working?

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or ...READ MORE

Jul 18, 2019 in Python by Waseem
• 4,540 points
2,056 views
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
10,912 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
2,446 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
108,236 views
–1 vote
1 answer

Hadoop dfs -ls command?

In your case there is no difference ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by kurt_cobain
• 9,390 points
4,537 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP