小编tho*_*yon的帖子

Python列表目录,子目录和文件

我正在尝试创建一个脚本来列出给定目录中的所有目录,子目录和文件.
我试过这个:

import sys,os

root = "/home/patate/directory/"
path = os.path.join(root, "targetdirectory")

for r,d,f in os.walk(path):
    for file in f:
        print os.path.join(root,file)
Run Code Online (Sandbox Code Playgroud)

不幸的是,它无法正常工作.
我得到了所有文件,但不是完整的路径.

例如,如果dir结构是:

/home/patate/directory/targetdirectory/123/456/789/file.txt

它会打印:

/home/patate/directory/targetdirectory/file.txt

我需要的是第一个结果.任何帮助将不胜感激!谢谢.

python file path

105
推荐指数
7
解决办法
16万
查看次数

Python迭代

我正在尝试在Python中创建一个简单的脚本,它将打印十六进制值并增加值,如下所示:

 char = 0
 char2 = 0

 def doublehex():
    global char,char2
    for x in range(255):
        char = char + 1
        a = str(chr(char)).encode("hex")
        for p in range(255):
           char2 = char2 + 1
           b = str(chr(char2)).encode("hex")
           c = a+" "+b
           print "testing with:%s"%(c)
doublehex()
Run Code Online (Sandbox Code Playgroud)

输出:

testing with:01 01
testing with:01 02
testing with:01 03
[snip]
testing with:01 fd
testing with:01 fe
testing with:01 ff

Traceback (most recent call last):
  File "test2.py", line 16, in doublehex
   b = str(chr(char2)).encode("hex")
ValueError: chr() arg …
Run Code Online (Sandbox Code Playgroud)

python hex loops for-loop increment

3
推荐指数
1
解决办法
2891
查看次数

标签 统计

python ×2

file ×1

for-loop ×1

hex ×1

increment ×1

loops ×1

path ×1