0

I need to translate chunks of matlab code into Python. My code seems to be 'unreachable' though. Any idea why this is happening? Also: am I doing it right? I'm a real newbie.

Matlab code:

function Dir = getScriptDir()
 fullPath = mfilename('fullpath');
 [Dir, ~,~] = fileparts(fullPath);
end
function [list,listSize] = getFileList(Dir)
 DirResult = dir( Dir );
 list = DirResult(~[DirResult.isdir]); % select files
 listSize = size(list);
end

My Python code:

def Dir = getScriptDir():
 return os.path.dirname(os.path.realpath(__file__)
def getFileList(Dir):
 list = os.listdir(Dir)
 listSize = len(list)
 getFileList() = [list, listSize]
asked Sep 9, 2016 at 14:32
6
  • 1
    getFileList() = [list, listSize] does not seems like correct code. Maybe return [list, listSize] ? Also, using camelCase named functions is violates python code standart Commented Sep 9, 2016 at 14:37
  • @IlyaBoltnev What is "standart"? Commented Sep 9, 2016 at 14:49
  • @IlyaBoltnev can the upper case characters impact my code? it won't work if I don't change it? Commented Sep 9, 2016 at 14:53
  • @glglgl i mean pep8, thec code style standart Commented Sep 9, 2016 at 15:31
  • @IlyaBoltnev It was meant as a slight hint that standard is written with two "d". Just trying to be a smartass. :-P Commented Sep 12, 2016 at 8:55

3 Answers 3

3

Your syntax is incorrect. If I'm reading this correctly, you're trying to get the names of the files in the same directory as the script and print the number of files in that list.


Here's an example of how you might do this (based on the program you gave):

import os
def getFileList(directory = os.path.dirname(os.path.realpath(__file__))):
 list = os.listdir(directory)
 listSize = len(list)
 return [list, listSize]
print(getFileList())

Output example:

[['program.py', 'data', 'syntax.py'], 3]
answered Sep 9, 2016 at 14:40
Sign up to request clarification or add additional context in comments.

2 Comments

The solution of @ode2k works, too (sorry, I can't comment on your post yet because of my reputation), but the "getScriptDir" function isn't necessary. It's easier to use a keyword argument to set the directory (plus, the function is now re-usable with other directories).
thank you both, i needed that information also for the rest of the code :)))
1

Your function definitions were incorrect. I have modified the code you provided. You can also consolidate the getScriptDir() functionality into the getFileList() function.

import os
def getFileList():
 dir = os.path.dirname(os.path.realpath(__file__))
 list = os.listdir(dir)
 listSize = len(list)
 fileList = [list, listSize]
 return fileList
print(getFileList())

Returns: (in my environment)

[['test.py', 'test.txt', 'test2.py', 'test2.txt', 'test3.py', 'test4.py', 'testlog.txt', '__pycache__'], 8]

Your script functions - including getScriptDir(modified):

import os
def getScriptDir():
 return os.path.dirname(os.path.realpath(__file__))
def getFileList(dir):
 dir = os.path.dirname(os.path.realpath(__file__))
 list = os.listdir(dir)
 listSize = len(list)
 fileList = [list, listSize]
 return fileList
dir = getScriptDir()
print(getFileList(dir))
answered Sep 9, 2016 at 14:38

Comments

0

Remember that you need to return variables from a python-function to get their results.

More on how to define your own functions in python: https://docs.python.org/3/tutorial/controlflow.html#defining-functions

answered Sep 9, 2016 at 14:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.