Sponsored Content
Top Forums Programming Populating Lists in Def using Python Post 302869899 by metallica1973 on Thursday 31st of October 2013 11:49:56 AM
Old 10-31-2013
ok I got my function to work using:

Code:
def list_files():
    content = []
    for files in os.walk('var/www/html/data/customer/log'):
       content.extend(files)
    return content

next stupid question is how do I access one of the many files in the function? So the results of os.walk populates my list -- content [] with the files in the directory:

Code:
list_files()
print list_files()
['var/www/html/data/customer/log', [], ['file1', 'file2', 'file3', 'file4']]

so how would I access that information so I can add additional logic to my script? when I attempt to access anything in my list, I get the following error

Code:
content[3]

NameError: name 'content' is not defined

when not using a function I can access the elements a such:

Code:
In [66]: func = []

In [67]: func.extend( ("blah1","blah2" ) )

In [68]: print func
['blah1', 'blah2']
In [69]: print func[0]
blah1

In [70]: print func[1]
blah2


Last edited by metallica1973; 10-31-2013 at 01:19 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

.def file in HP-UX Shell scripting

Hi Pals, I need some information related .def file in HP-Ux shell scripting. What actaully a .def file contains. It is having all definitions of some functions. But what is the relationship between a .def file and shell script. Can anyone give some examples. Thanks in Advance. Best... (1 Reply)
Discussion started by: manu.vmr
1 Replies

2. Shell Programming and Scripting

Populating an Array

Guys, I need to iterate populate an array while going over files in directory. Can someone please tell me syntax I tried this but it isn't working ==> for F in `ls -p "${directory1}" | grep -v "\/"` do cd "${directory2}" cmp "${directory2}"/"${F}" "${directory1}"/"${F}" ... (2 Replies)
Discussion started by: Veenak15
2 Replies

3. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

4. Programming

populating a JList

Hi, I have to create a JList and the items I need to display are store in HashMap table. What would be the easiest way to populate this JList. Basically the items I want to display/show in the JList are the key values of the HashMap. Thanks in advance for any suggestions. (0 Replies)
Discussion started by: arizah
0 Replies

5. Programming

Python: Compare 2 word lists

Hi. I am trying to write a Python programme that compares two different text files which both contain a list of words. Each word has its own line worda wordb wordc I want to compare textfile 2 with textfile 1, and if there's a word in textfile 2 that is NOT in textfile 1, I want to... (6 Replies)
Discussion started by: Bloomy
6 Replies

6. Homework & Coursework Questions

[Python] Compare 2 lists

Hello, I'm new to the python programming, and I have a question. I have to write a program that prints a receipt for a restaurant. The input is a list which looks like: product1 product3 product8 .... In the other input file there is a list which looks like: product1 coffee 5,00... (1 Reply)
Discussion started by: dagendy
1 Replies

7. Shell Programming and Scripting

Separate Text File into Two Lists Using Python

Hello, I have a pretty simple question, but I am new to Python and am trying to write a simple program. Put simply, I want to take a text file that looks like this: 11111 22222 33333 44444 55555 66666 77777 88888 and produce two lists, one containing the contents of the left column, one the... (0 Replies)
Discussion started by: Tyler_92
0 Replies

8. Programming

Python Concatenating 2 Lists

I am just trying to concatenate two lists together, but I am not sure what is wrong with my code, it won't run. Thank you for any help. #!/usr/bin/python # Takes two lists and returns a list that is the concatenation of both of # them. A = B = def list_concat( A, B): print(A) ... (2 Replies)
Discussion started by: totoro125
2 Replies

9. Homework & Coursework Questions

KLIBC .def file

I am working on klibc. I need to add a new command in kernel. klibc contains a SYSCALLS.def file which declares functions. Where can I find definition of functions declared in .def file? (1 Reply)
Discussion started by: Vasundhara08
1 Replies
PYTHON(1)						    BSD General Commands Manual 						 PYTHON(1)

NAME
python, pythonw -- an interpreted, interactive, object-oriented programming language SYNOPSIS
python ... pythonw ... DESCRIPTION
To support multiple versions, the programs named python and pythonw now just select the real version of Python to run, depending on various settings. (As of Python 2.5, python and pythonw are interchangeable; both execute Python in the context of an application bundle, which means they have access to the Graphical User Interface; thus both can, when properly programmed, display windows, dialogs, etc.) The current supported versions are 2.6 and 2.7, with the default being 2.6. Use % man python2.6 % man python2.7 % man pythonw2.6 % man pythonw2.7 to see the man page for a specific version. Without a version specified, % man pydoc and the like, will show the man page for the (unmodified) default version of Python (2.6). To see the man page for a specific version, use, for example, % man pydoc2.7 CHANGING THE DEFAULT PYTHON
Using % defaults write com.apple.versioner.python Version 2.7 will make version 2.7 the user default when running the both the python and pythonw commands (versioner is the internal name of the version- selection software used). To set a system-wide default, replace 'com.apple.versioner.python' with '/Library/Preferences/com.apple.versioner.python' (admin privileges will be required). The environment variable VERSIONER_PYTHON_VERSION can also be used to set the python and pythonw version: % export VERSIONER_PYTHON_VERSION=2.7 # Bourne-like shells or % setenv VERSIONER_PYTHON_VERSION 2.7 # C-like shells % python ... This environment variable takes precedence over the preference file settings. 64-BIT SUPPORT Versions 2.6 and 2.7 support 64-bit execution (which is on by default). Like the version of Python, the python command can select between 32 and 64-bit execution (when both are available). Use: % defaults write com.apple.versioner.python Prefer-32-Bit -bool yes to make 32-bit execution the user default (using '/Library/Preferences/com.apple.versioner.python' will set the system-wide default). The environment variable VERSIONER_PYTHON_PREFER_32_BIT can also be used (has precedence over the preference file): % export VERSIONER_PYTHON_PREFER_32_BIT=yes # Bourne-like shells or % setenv VERSIONER_PYTHON_PREFER_32_BIT yes # C-like shells Again, the preference setting and environmental variable applies to both python and pythonw. USING A SPECIFIC VERSION
Rather than using the python command, one can use a specific version directly. For example, running python2.7 from the command line will run the 2.7 version of Python, independent of what the default version of Python is. One can use a specific version of Python on the #! line of a script, but that may have portability and future compatibility issues. Note that the preference files and environment variable that apply to the python command, do not apply when running a specific version of Python. In particular, running python2.6 will always default to 64-bit execution (unless one uses the arch(1) command to specifically select a 32-bit architecture). SEE ALSO
python2.6(1), python2.7(1), pythonw2.6(1), pythonw2.7(1), arch(1) BSD
Aug 10, 2008 BSD
All times are GMT -4. The time now is 02:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy