I'm trying to make a pygame executable. I will use the following setup file:
我正在尝试制作一个pygame可执行文件。我将使用以下安装文件:
from distutils.core import setup
import py2exe
setup(
console = [ "game.py" ],
author = "me",
data_files = [ ( directory, [ file_1, file_2... ] ) ]
)
I've gotten this to work fine on a simple game, but the game I want to package up has > 700 image files. My question is, is there a simple way to read all file names from a directory so I don't have to list each one separately?
我已经让它在一个简单的游戏上工作得很好,但我想打包的游戏有> 700个图像文件。我的问题是,是否有一种简单的方法从目录中读取所有文件名,所以我不必单独列出每个文件名?
While there are some files that are numbered in sequence, most have a different root name, so writing a loop that makes the appropriate strings is not an option.
虽然有些文件按顺序编号,但大多数都有不同的根名称,因此编写一个生成相应字符串的循环不是一种选择。
Thanks
谢谢
4 个解决方案
#1
6
import os
data_files = [(x[0], x[2]) for x in os.walk(root_dir)]
http://docs.python.org/library/os.html#os.walk
http://docs.python.org/library/os.html#os.walk
This will give you the files for every subdirectory contained within root_dir
(along with the ones in root_dir
itself, of course) as tuples in the format [(dirpath1, [file1, file2, ...]), (dirpath2, [file3, file4, ...]), ...]
这将为你提供root_dir中包含的每个子目录的文件(以及root_dir本身的子目录),作为[[dirpath1,[file1,file2,...])格式的元组,(dirpath2,[file3, file4,...]),...]
I gave you this as my answer because I'm assuming (or rather, hoping) that, with so many files, you're keeping them properly organized rather than having them all in a single directory with no subdirectories.
我给你这个作为我的答案,因为我假设(或者更确切地说,希望)有这么多文件,你保持它们正确组织而不是将它们全部放在没有子目录的单个目录中。
It's been a while, but coming back to this I now realize that for a long list of files, a generator expression would probably be more efficient. To do so, a very simple change is needed; replace the outer brackets with parentheses, i.e.
已经有一段时间了,但回过头来看,我现在意识到,对于一长串文件,生成器表达式可能会更有效率。要做到这一点,需要进行非常简单的改变;用括号替换外括号,即
import os
data_file_gen = ((x[0], x[2]) for x in os.walk(root_dir))
And data_file_gen
could then be used wherever data_files
was iterated over (assuming you're only using the generator once; if the generator is to be used multiple times, you would have to recreate the generator for each use. In such cases it may be better to just use the list method unless memory does become a problem.).
然后可以在迭代data_files的任何地方使用data_file_gen(假设您只使用一次生成器;如果要多次使用生成器,则必须为每次使用重新创建生成器。在这种情况下,它可能更好只使用list方法,除非内存确实成为一个问题。)
#2
4
Try this
尝试这个
data_files = [directory, ...] # whatever you want to put specifically.
data_files += [file for file in os.listdir('images') if os.path.isfile(file)] # for adding your image files
#3
4
The os
module has a method called listdir
that takes a path and returns the contents as a list.
os模块有一个名为listdir的方法,它接受一个路径并将内容作为列表返回。
#4
1
Use glob to collect all files in a directory. Use . to collect all files, or *.jpg to collect all .jpg files. This will put the images in a folder called "Images" in your py2exe output directory.
使用glob来收集目录中的所有文件。使用 。收集所有文件,或* .jpg收集所有.jpg文件。这会将图像放在py2exe输出目录中名为“Images”的文件夹中。
...
from glob import glob
...
data_files = [
("Stuff", glob(r'C:\projectfolder\Stuff\*.*'))
,("dlls", glob(r'C:\projectfolder\dlls\*.dll'))
,("Images", glob(r'C:\projectfolder\images\*.*'))
]
...
setup(
name='ProjectName'
,options = options
,data_files=data_files
,console=['projectname.py']
)
#1
6
import os
data_files = [(x[0], x[2]) for x in os.walk(root_dir)]
http://docs.python.org/library/os.html#os.walk
http://docs.python.org/library/os.html#os.walk
This will give you the files for every subdirectory contained within root_dir
(along with the ones in root_dir
itself, of course) as tuples in the format [(dirpath1, [file1, file2, ...]), (dirpath2, [file3, file4, ...]), ...]
这将为你提供root_dir中包含的每个子目录的文件(以及root_dir本身的子目录),作为[[dirpath1,[file1,file2,...])格式的元组,(dirpath2,[file3, file4,...]),...]
I gave you this as my answer because I'm assuming (or rather, hoping) that, with so many files, you're keeping them properly organized rather than having them all in a single directory with no subdirectories.
我给你这个作为我的答案,因为我假设(或者更确切地说,希望)有这么多文件,你保持它们正确组织而不是将它们全部放在没有子目录的单个目录中。
It's been a while, but coming back to this I now realize that for a long list of files, a generator expression would probably be more efficient. To do so, a very simple change is needed; replace the outer brackets with parentheses, i.e.
已经有一段时间了,但回过头来看,我现在意识到,对于一长串文件,生成器表达式可能会更有效率。要做到这一点,需要进行非常简单的改变;用括号替换外括号,即
import os
data_file_gen = ((x[0], x[2]) for x in os.walk(root_dir))
And data_file_gen
could then be used wherever data_files
was iterated over (assuming you're only using the generator once; if the generator is to be used multiple times, you would have to recreate the generator for each use. In such cases it may be better to just use the list method unless memory does become a problem.).
然后可以在迭代data_files的任何地方使用data_file_gen(假设您只使用一次生成器;如果要多次使用生成器,则必须为每次使用重新创建生成器。在这种情况下,它可能更好只使用list方法,除非内存确实成为一个问题。)
#2
4
Try this
尝试这个
data_files = [directory, ...] # whatever you want to put specifically.
data_files += [file for file in os.listdir('images') if os.path.isfile(file)] # for adding your image files
#3
4
The os
module has a method called listdir
that takes a path and returns the contents as a list.
os模块有一个名为listdir的方法,它接受一个路径并将内容作为列表返回。
#4
1
Use glob to collect all files in a directory. Use . to collect all files, or *.jpg to collect all .jpg files. This will put the images in a folder called "Images" in your py2exe output directory.
使用glob来收集目录中的所有文件。使用 。收集所有文件,或* .jpg收集所有.jpg文件。这会将图像放在py2exe输出目录中名为“Images”的文件夹中。
...
from glob import glob
...
data_files = [
("Stuff", glob(r'C:\projectfolder\Stuff\*.*'))
,("dlls", glob(r'C:\projectfolder\dlls\*.dll'))
,("Images", glob(r'C:\projectfolder\images\*.*'))
]
...
setup(
name='ProjectName'
,options = options
,data_files=data_files
,console=['projectname.py']
)