read_files_in_dir
时间: 2023-11-30 09:04:47 浏览: 131
您的问题是关于读取文件目录的函数,是吗?那么我可以为您提供一个 Python 的示例代码:
```
import os
def read_files_in_dir(directory):
files = []
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
if os.path.isfile(path):
with open(path, 'r') as f:
content = f.read()
files.append(content)
return files
```
这个函数可以读取指定目录下的所有文件,并将每个文件的内容存储在一个列表中返回。您只需要将 `directory` 参数替换为您想要读取的目录的路径即可。希望这可以帮到您!
相关问题
def read_files(data_dir):#按照行读取文件,去除空格,按照行将文件数据记录在列表中,返回每个列表 with open(data_dir , 'r') as f: pos_pos = f.readlines()#一行一行读取文件内容,并返回一个列表 pos_pos = [list(map(int, sen.split(' '))) for sen in pos_pos]data_dir ="D:/Anaconda/Jupyter_Notebook/TIPRDC-main/Text/twitteraae_limited_aa" t=read_files(data_dir),这段代码出现问题ValueError: invalid literal for int() with base 10: '293846693215096832\t"Tue'
这个问题可能是因为你在尝试将一个字符串转换为整数时出现了错误,因为字符串中包含了除数字以外的其他字符。你可以尝试检查一下数据文件中的每一行,看看其中是否包含了非数字字符。如果是这样的话,你需要先将这些非数字字符去除掉,然后再进行转换。你可以使用Python中的正则表达式来去除非数字字符,例如:
```
import re
def read_files(data_dir):
with open(data_dir , 'r') as f:
pos_pos = f.readlines()
pos_pos = [re.sub(r'\D', ' ', sen).strip() for sen in pos_pos] # 去除非数字字符
pos_pos = [list(map(int, sen.split())) for sen in pos_pos]
return pos_pos
data_dir ="D:/Anaconda/Jupyter_Notebook/TIPRDC-main/Text/twitteraae_limited_aa"
t = read_files(data_dir)
```
这段代码中,`re.sub(r'\D', ' ', sen)` 会将字符串 `sen` 中的非数字字符替换为空格,然后用 `strip()` 函数去掉字符串两端的空格,最后再将字符串分割成一个个整数。
cmake embed_txtfiles
`embed_txtfiles` 是一个自定义 CMake 函数,用于将多个文本文件嵌入到可执行文件中,以便在运行时读取这些文件内容。下面是一个示例函数的实现:
```cmake
function(embed_txtfiles target_name)
set(output_file "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_embedded_files.c")
set(files_var_name "${target_name}_EMBEDDED_FILES")
set(var_name "${target_name}_EMBEDDED_FILES_DATA")
# Collect the file contents and embed them as char arrays
set(srcs "")
set(${var_name} "")
foreach(file ${ARGN})
file(READ ${file} contents)
string(REPLACE "\"" "\\\"" contents "${contents}")
string(REPLACE "\n" "\\n\"\n\"" contents "${contents}")
list(APPEND srcs "${CMAKE_CURRENT_BINARY_DIR}/temp/${file}.c")
set(${var_name} "${${var_name}}static const char ${file}_data[] = \"${contents}\";\n")
set(${var_name} "${${var_name}}static const size_t ${file}_size = ${CMAKE_CURRENT_BINARY_DIR}/temp/${file}_size;\n")
endforeach()
# Generate the C code that will be compiled into the executable
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/temp")
file(WRITE ${output_file} "${${var_name}}\n")
foreach(file ${ARGN})
file(SIZE ${file} size)
set(size_var "${CMAKE_CURRENT_BINARY_DIR}/temp/${file}_size")
file(WRITE ${size_var} "${size}")
file(APPEND ${output_file} "extern const char ${file}_data[];\n")
file(APPEND ${output_file} "extern const size_t ${file}_size;\n")
endforeach()
file(APPEND ${output_file} "\nvoid ${target_name}_init() {\n")
foreach(file ${ARGN})
file(APPEND ${output_file} " resource_manager_add_resource(\"${file}\", ${file}_data, ${file}_size);\n")
endforeach()
file(APPEND ${output_file} "}\n")
# Compile the generated C code into a library that can be linked into the target
add_library(${files_var_name} STATIC ${srcs})
target_compile_definitions(${files_var_name} PRIVATE ${var_name}=${var_name})
target_include_directories(${files_var_name} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/temp")
# Add a custom command to generate the C code and compile it before building the target
add_custom_command(
OUTPUT ${srcs}
COMMAND ${CMAKE_COMMAND} -E copy ${ARGN} "${CMAKE_CURRENT_BINARY_DIR}/temp"
COMMAND ${CMAKE_COMMAND} -E touch ${output_file}
DEPENDS ${ARGN}
COMMENT "Embedding TXT files in ${target_name}"
)
# Add the custom command as a dependency of the target
add_dependencies(${target_name} ${files_var_name})
# Link the embedded files library into the target
target_link_libraries(${target_name} ${files_var_name})
endfunction()
```
使用此函数的示例:
```cmake
add_executable(myapp main.c)
embed_txtfiles(myapp data/file1.txt data/file2.txt)
```
在这个示例中,`embed_txtfiles` 函数将 `data/file1.txt` 和 `data/file2.txt` 嵌入到 `myapp` 可执行文件中。在运行时,可以使用 `resource_manager_get_resource("data/file1.txt")` 和 `resource_manager_get_resource("data/file2.txt")` 来获取这两个文件的内容。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)