Jump to content

I want to make a python script for py2exe. i would like it to get the parent directory and all files in a the folder. I know i have to put it into a for loop but i don't know how to code it.

dataFiles = [["parentFolder/anySubdirectories",["parentFolder/anySubdirectories/myBackground.png"]], ["parentFolder/anySubdirectories",["parentFolder/anySubdirectories/myFile2.txt"]]] # and so on ...

 

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/
Share on other sites

Link to post
Share on other sites

I think @fizzlesticks might be able to help.

 

Never mind he has not been on for ages.

 

Oh, he is here. Hahah

 

Also, try to reformat it using the "code" button at the top of the textbox when editing your post :)

 

It will be much easier to read then.

CPU: i5 4670k @ 3.4GHz + Corsair H100i      GPU: Gigabyte GTX 680 SOC (+215 Core|+162 Mem)     SSD: Kingston V300 240GB (OS)      Headset: Logitech G930 

Case: Cosair Vengance C70 (white)                RAM: 16GB TeamGroup Elite Black DDR3 1600MHz       HDD: 1TB WD Blue                              Mouse: Logitech G602

OS: Windows 7 Home Premium                       PSUXFX Core Edition 750w                                                Motherboard: MSI Z97-G45               Keyboard: Logitech G510

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7611847
Share on other sites

Link to post
Share on other sites

1 hour ago, TheGamingHD said:

I want to make a python script for py2exe. i would like it to get the parent directory and all files in a the folder. I know i have to put it into a for loop but i don't know how to code it.


dataFiles = [["parentFolder/anySubdirectories",["parentFolder/anySubdirectories/myBackground.png"]], ["parentFolder/anySubdirectories",["parentFolder/anySubdirectories/myFile2.txt"]]] # and so on ...

 

I am not used to python on windows but I would look at the glob library (https://docs.python.org/3/library/glob.html) it has a argument to make it recursive so you dont have to make a recursive function your self.

 

You can ether loop it in a for file in glob('parentFolder/*', recursive=True) to get all the folders and files. or you can get all the files from every sub folder (I also see you want png files so I added that as-well) glob('parentFolder/*/*.{png,txt}') this should give you a nice list of files.

 

can the array be flat or do you want to keep the folder structure intact ?

 

If you need more help let me know your specific case and will write a small script for you to demonstrate.

 

 

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7612081
Share on other sites

Link to post
Share on other sites

After taking a better look at your requested array structure I wrote a small script that will give the output you desire.

(The code is not the most optimal and speedy but wrote it for clarity, more then anything else. so you can write your own)

import glob
import os.path

def directory_listing_recursive(path):
    files = []
    for item in glob.glob(path + '/*'):
        if os.path.isdir(item):
            files.append([item, directory_listing_recursive(item)])
        else:
            files.append(item)
    return files

print(directory_listing_recursive(os.path.realpath('./test')))

Tested with the following folder structure 

[arekana@arekanapro /var/code/python/playground/recursive]$ ls -lhR
total 8
-rw-r--r--  1 arekana  wheel   351B Apr 17 23:31 main.py
drwxr-xr-x  4 arekana  wheel   136B Apr 17 23:14 test

./test:
total 0
drwxr-xr-x  4 arekana  wheel   170B Apr 17 23:15 sub1
drwxr-xr-x  2 arekana  wheel   102B Apr 17 23:15 sub2

./test/sub1:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:15 file1_1
drwxr-xr-x  2 arekana  wheel   102B Apr 17 23:14 sub1_1
drwxr-xr-x  2 arekana  wheel   102B Apr 17 23:15 sub1_2

./test/sub1/sub1_1:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:14 file1_1_1.txt

./test/sub1/sub1_2:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:15 file1_2_1.txt

./test/sub2:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:15 file2_1.txt

it will give the following output

 

[['/private/var/code/python/playground/recursive/test/sub1', ['/private/var/code/python/playground/recursive/test/sub1/file1_1', ['/private/var/code/python/playground/recursive/test/sub1/sub1_1', ['/private/var/code/python/playground/recursive/test/sub1/sub1_1/file1_1_1.txt']], ['/private/var/code/python/playground/recursive/test/sub1/sub1_2', ['/private/var/code/python/playground/recursive/test/sub1/sub1_2/file1_2_1.txt']]]], ['/private/var/code/python/playground/recursive/test/sub2', ['/private/var/code/python/playground/recursive/test/sub2/file2_1.txt']]]

Although if your array structure does not matter to you then I still suggest using glob with the build-in recursive argument to do it for you as it a bit nicer.

 

If you require anymore help do not hesitate to ask.

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7612219
Share on other sites

Link to post
Share on other sites

8 hours ago, arekana said:

After taking a better look at your requested array structure I wrote a small script that will give the output you desire.

(The code is not the most optimal and speedy but wrote it for clarity, more then anything else. so you can write your own)


import glob
import os.path

def directory_listing_recursive(path):
    files = []
    for item in glob.glob(path + '/*'):
        if os.path.isdir(item):
            files.append([item, directory_listing_recursive(item)])
        else:
            files.append(item)
    return files

print(directory_listing_recursive(os.path.realpath('./test')))

Tested with the following folder structure 


[arekana@arekanapro /var/code/python/playground/recursive]$ ls -lhR
total 8
-rw-r--r--  1 arekana  wheel   351B Apr 17 23:31 main.py
drwxr-xr-x  4 arekana  wheel   136B Apr 17 23:14 test

./test:
total 0
drwxr-xr-x  4 arekana  wheel   170B Apr 17 23:15 sub1
drwxr-xr-x  2 arekana  wheel   102B Apr 17 23:15 sub2

./test/sub1:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:15 file1_1
drwxr-xr-x  2 arekana  wheel   102B Apr 17 23:14 sub1_1
drwxr-xr-x  2 arekana  wheel   102B Apr 17 23:15 sub1_2

./test/sub1/sub1_1:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:14 file1_1_1.txt

./test/sub1/sub1_2:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:15 file1_2_1.txt

./test/sub2:
total 0
-rw-r--r--  1 arekana  wheel     0B Apr 17 23:15 file2_1.txt

it will give the following output

 


[['/private/var/code/python/playground/recursive/test/sub1', ['/private/var/code/python/playground/recursive/test/sub1/file1_1', ['/private/var/code/python/playground/recursive/test/sub1/sub1_1', ['/private/var/code/python/playground/recursive/test/sub1/sub1_1/file1_1_1.txt']], ['/private/var/code/python/playground/recursive/test/sub1/sub1_2', ['/private/var/code/python/playground/recursive/test/sub1/sub1_2/file1_2_1.txt']]]], ['/private/var/code/python/playground/recursive/test/sub2', ['/private/var/code/python/playground/recursive/test/sub2/file2_1.txt']]]

Although if your array structure does not matter to you then I still suggest using glob with the build-in recursive argument to do it for you as it a bit nicer.

 

If you require anymore help do not hesitate to ask.

I have tried using your code with my program and i am getting a value error 2.

Traceback (most recent call last):
  File "setup.py", line 32, in <module>
    data_files=directory_listing_recursive(os.path.realpath('./'))
  File "C:\Program Files\Python\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Program Files\Python\lib\distutils\dist.py", line 955, in run_command
s
    self.run_command(cmd)
  File "C:\Program Files\Python\lib\distutils\dist.py", line 974, in run_command

    cmd_obj.run()
  File "C:\Program Files\Python\lib\site-packages\py2exe\distutils_buildexe.py",
 line 188, in run
    self._run()
  File "C:\Program Files\Python\lib\site-packages\py2exe\distutils_buildexe.py",
 line 268, in _run
    builder.build()
  File "C:\Program Files\Python\lib\site-packages\py2exe\runtime.py", line 278,
in build
    for subdir, files in self.options.data_files:
ValueError: too many values to unpack (expected 2)

 

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7614030
Share on other sites

Link to post
Share on other sites

1 hour ago, TheGamingHD said:

I have tried using your code with my program and i am getting a value error 2.


Traceback (most recent call last):
  File "setup.py", line 32, in <module>
    data_files=directory_listing_recursive(os.path.realpath('./'))
  File "C:\Program Files\Python\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Program Files\Python\lib\distutils\dist.py", line 955, in run_command
s
    self.run_command(cmd)
  File "C:\Program Files\Python\lib\distutils\dist.py", line 974, in run_command

    cmd_obj.run()
  File "C:\Program Files\Python\lib\site-packages\py2exe\distutils_buildexe.py",
 line 188, in run
    self._run()
  File "C:\Program Files\Python\lib\site-packages\py2exe\distutils_buildexe.py",
 line 268, in _run
    builder.build()
  File "C:\Program Files\Python\lib\site-packages\py2exe\runtime.py", line 278,
in build
    for subdir, files in self.options.data_files:
ValueError: too many values to unpack (expected 2)

 

After trying it my self

 

for subdir, files in folder_structure_intact_glob(os.path.realpath('./test')): print(subdir) print(files)

 

It seem to work, what version of python are you using ? 

Can you dump self.options.data_files to see if the array is still intact.

 

My only other guess could be that it might be windows (as mentioned don't know anything about python on windows or windows in general).

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7614191
Share on other sites

Link to post
Share on other sites

1 hour ago, arekana said:

After trying it my self

 


for subdir, files in folder_structure_intact_glob(os.path.realpath('./test')): print(subdir) print(files)

 

It seem to work, what version of python are you using ? 

Can you dump self.options.data_files to see if the array is still intact.

 

My only other guess could be that it might be windows (as mentioned don't know anything about python on windows or windows in general).

I am using python 3.4, here's the output:

http://pastebin.com/t0LRt2JF

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7614397
Share on other sites

Link to post
Share on other sites

12 hours ago, arekana said:

After trying it my self

 


for subdir, files in folder_structure_intact_glob(os.path.realpath('./test')): print(subdir) print(files)

 

It seem to work, what version of python are you using ? 

Can you dump self.options.data_files to see if the array is still intact.

 

My only other guess could be that it might be windows (as mentioned don't know anything about python on windows or windows in general).

@arekana After many hours of trying, I have got it to work but it adds the file twice?

running py2exe

  1 missing Modules
  ------------------
? readline                            imported from cmd, code, pdb
Building 'dist\run.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy C:\Program Files\Python\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy C:\Program Files\Python\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy C:\Program Files\Python\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy C:\Program Files\Python\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy C:\Program Files\Python\DLLs\select.pyd to dist\select.pyd
Copy C:\Program Files\Python\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy C:\Program Files\Python\DLLs\_bz2.pyd to dist\_bz2.pyd
Copy C:\Program Files\Python\DLLs\_socket.pyd to dist\_socket.pyd
Copy C:\Program Files\Python\DLLs\pyexpat.pyd to dist\pyexpat.pyd
error: 'C:\\Documents and Settings\\Jordon\\PycharmProjects\\Glass_OS\\GlassOS\\
boot.py' and 'C:\\Documents and Settings\\Jordon\\PycharmProjects\\Glass_OS\\Gla
ssOS\\boot.py' are the same file

 

Link to comment
https://linustechtips.com/topic/583361-help-with-python-script/#findComment-7616679
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×